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

Tsol
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2012

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
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2012

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.

Tsol
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2012

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

Todd Winton July 15, 2015

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?

2 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2013

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.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2013

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

Tsol
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2013

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

Like SHANAVASH MOHAMED likes this
Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2013

@Renjith,

Could you please explain exactly on how to do this?

Thanks,

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2013

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

Thanks in advance,

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2013

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?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 27, 2013

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.

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 27, 2013

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,

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 27, 2013

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.

Kathryn Allison January 7, 2014

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

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

Kathryn Allison January 8, 2014

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

0 votes
Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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,

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

Go for a listener then.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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?

Vishali
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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

Regards,

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2013

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