Calculate value for custom field on updating issue

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Calculate value for custom field on updating issue

Post by pollevr »

Hi all,

in Mantis v. 2.24.1 I'm trying to write a plugin for setting a value calculated automatically, based on the values of other fields or custom fields.

In my plugin there are following code:

Code: Select all

class MyPluginPlugin extends MantisPlugin {
	function register() {
		...
	}
	function hooks(){
		return array(
			'EVENT_MYPLUGIN_UPDATE'      => 'update',
		);
	}
	function events() {
		return array(
			'EVENT_MYPLUGIN_UPDATE'      => EVENT_UPDATE_BUG,
		);
	}
	function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
		$t_id = custom_field_get_id_from_name('<my_custom_field_name>');
		custom_field_set_value( $t_id , $p_issue_id, <my_new_value>); 
		return $p_bug_data;
	}
	...
}
When I update an issue I'll expect that the custom field <my_custom_field_name> will save with the <my_new_value>.

Why not?


Thanks all,
Cristian
atrol
Site Admin
Posts: 8366
Joined: 26 Mar 2008, 21:37
Location: Germany

Re: Calculate value for custom field on updating issue

Post by atrol »

Should work, didn't try

Code: Select all

class MyPluginPlugin extends MantisPlugin {

	function hooks(){
		return array(
			'EVENT_UPDATE_BUG'      => 'update',
		);
	}

	function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
		$t_id = custom_field_get_id_from_name('<my_custom_field_name>');
		custom_field_set_value( $t_id , $p_issue_id, <my_new_value>); 
		return $p_bug_data;
	}
	...
}
Please use Search before posting and read the Manual
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Hi atrol,

thank you for your suggestion, but unfortunately doesn't work.

I don't understand the difference between your an my code.

my code:

Code: Select all

function hooks(){
	return array(
		'EVENT_MYPLUGIN_UPDATE'      => 'update',
	);
}
function events() {
	return array(
		'EVENT_MYPLUGIN_UPDATE'      => EVENT_UPDATE_BUG,
	);
}
function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
	$t_id = custom_field_get_id_from_name('<my_custom_field_name>');
	custom_field_set_value( $t_id , $p_issue_id, <my_new_value>); 
	return $p_bug_data;
}
your code:

Code: Select all

function hooks(){
	return array(
		'EVENT_UPDATE_BUG'      => 'update',
	);
}

function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
	$t_id = custom_field_get_id_from_name('<my_custom_field_name>');
	custom_field_set_value( $t_id , $p_issue_id, <my_new_value>); 
	return $p_bug_data;
}
I think that with my code I can call my custom function "update", what is the difference from your code? Is the same thing?
In any case neither one nor the other works.
Any other suggestions?

Thank you,
Cristian
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Hi atrol,

I took a small step forward.

Now my code is:

Code: Select all

function init() {
	plugin_event_hook( 'EVENT_UPDATE_BUG', 'update' );
}

function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
	$t_id = custom_field_get_id_from_name('<my_custom_field_name');
	//custom_field_set_value( $t_id , $p_issue_id, <any_integer_value>); 
	custom_field_set_value( $t_id , 30, <any_integer_value>);
	return $p_bug_data;
}
I added in function init and I removed from functions events and hooks and the update function is called correctly now!

But now the problem is that the id parameter $p_issue_id does not setted and therefore I don't know how to update the current issue (30 is an id that I know, but why $p_issue_id != 30 that is the issue I'm trying to change for test?)

Any ideas?

Thank you,
Cristian
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

I found it:

Code: Select all

function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {...}
$p_issue_id = EVENT_UPDATE_BUG
$p_bug_data = <my_id_issue_that_I_updated>

Thank you,
Cristian
Post Reply