It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Tsol Dec 02, 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 Dec 02, 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 Dec 02, 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 Jul 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 Feb 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 Feb 15, 2013

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

Tsol Feb 18, 2013

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

Vishali Feb 19, 2013

@Renjith,

Could you please explain exactly on how to do this?

Thanks,

Vishali Feb 25, 2013

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

Thanks in advance,

Vishali Feb 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 Feb 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 Feb 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 Feb 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 Jan 07, 2014

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

JamieA Jan 08, 2014

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

Kathryn Allison Jan 08, 2014

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

0 votes
Vishali Feb 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 Feb 13, 2013

Go for a listener then.

JamieA Feb 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 Feb 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 Feb 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 Feb 13, 2013

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

Regards,

JamieA Feb 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

2,133 views 6 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you