Post-function to sum the values of two number custom fields using groovyrunner?

Hi everybody,

i wan to create a post-function that will will sum the values of two number custom fields. A third number field will take the value of the sum. I have made some tests using script runner where i sum the values the two fields for a certain issue but i don't know how to make it work for a workflow transition. The code i'm using in script runner is the following

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
 

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
 

Issue issue = issueManager.getIssueObject( "RF-9944" );
CustomField customField_amdtl = customFieldManager.getCustomFieldObjectByName( "Amount Due to Losses" );
CustomField customField_amdtfr = customFieldManager.getCustomFieldObjectByName( "Amount Due to Full Refund" );
 

Object fam = issue.getCustomFieldValue( customField_amdtl ) + issue.getCustomFieldValue( customField_amdtfr );

Thanks in advance,

Kostas

3 answers

1 accepted

3 votes
Accepted answer

You should just be able to delete the where you define and initialise the issue variable. That will be set in the script binding. But you need to call setCustomFieldValue or something.

Thanx for the help.

The code i have used is the following:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
com.atlassian.jira.issue.MutableIssue;
 
// gets a reference to the IssueManager and CustomFieldManager manager classes
IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
 
// gets a reference to the desired issue and custom field

CustomField customField_amdtl = customFieldManager.getCustomFieldObjectByName( "Amount Due to Losses" );
CustomField customField_amdtfr = customFieldManager.getCustomFieldObjectByName( "Amount Due to Full Refund" );
CustomField customField_fam = customFieldManager.getCustomFieldObjectByName( "Final Amount" );
 
// retrieves the custom field value object from the issue
Object fam = issue.getCustomFieldValue( customField_amdtl ) + issue.getCustomFieldValue( customField_amdtfr );

issue.setCustomFieldValue(customField_fam, fam);

Cheers,

Kostas

Kostas, Should the same work if I am trying to subtract? So in the Object fam line, if I make the operator - should it subtract the value?

Do you really need a third custom field and listener. Why not just add a scripted field from Jamie's plugin and just show the sum of the first two ?One line code.

Agree... post-functions/listeners are overly complicating this.

The scripted field approach is much easier and better than the idea i had.

@Renjith,

Could you please explain exactly on how to do this?

Thanks,

Could somebody please help me in understanding on how to use this?

Thanks in advance,

I have a scripted field which calculates the sum of two fields and it works (calculates the sum) when I edit the issue as well.....but I should use the tab key on the keyboard. Otherwise, it wouldn't add up the values.

Script in scripted field:

def CE = getCustomFieldValue("CE")
def HOE = getCustomFieldValue("HOE")
def Sum = CE + HOE
if (CE) {
return Sum

}
else {
return null
}
if (HOE) {
return Sum

}
else {
return null
}

Am I missing something?

What do you mean "but I should use the tab key on the keyboard" - the field will only be displayed in view mode, so there is nothing to tab to.

Jamie,

While editing the custom field values, I have to use the tab key on my keyboard for the Sum value to be updated.

Thanks,

I don't really understand. If you want the field to be updated as soon as the user changes a field, but before they submit, you need to use javascript or the behaviours plugin or something.

I have no idea why the tab key would have any effect on a calculated custom field.

Is it possible to use this mechanism and also add 15% to the sum?

Isn't it just a case of multiplying by 1.15 ?

You're right...sorry for the silly question :)

Kostas,

Where did you paste this script? I have three number fields and I have used the script as a post-function at 'Create Issue'. During Edit or during any other transition's that I modify the values, there is no update to the Sum field.

Could you please help me?

Thanks,

Go for a listener then.

You'd need to add it as a listener. But updating a field is a bit more complicated in a listener. You can use very similar code in a scripted field: https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted+Fields

Jamie,

I have also used 'Final Amount' as a scripted field and used the same script at Administration->Scripted fields->Final Amount. I am not able to create the issue when I do this.

Thanks

Jamie,

Do you mean to say Script Listener or just the Atlassian Listener? Could you please point me to some documentation on how to achieve this?

Thank you so much for your immediate response. Let me check on how to do that. Have a nice day!

Regards,

Either really but I meant a script listener: https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 16, 2018 in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

1,149 views 12 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you