Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Script to sum two Number fields and display result in third field?

This should be very similar for you advanced scripters:  I have two Number fields ("Field A", "Field B") that I want to sum in a "Total" field.  I have script runner.  Can someone post for me the entire script that I could use to make this work?  I'm a weak scripter and need the entire script, not just excerpts.  Thanks you so much to anyone who can help.

This is similar to the question posted here, but I need someone to post the complete working script for me, which doesn't seem to have occurred in this entry:

2 answers

2 votes

For a full script to be developed, you'll need to be a lot more explicit with your requirements.

Where and when you want this to happen will have a pretty significant impact on how such a script would be implemented.

Some preliminary questions:

  1. Are all 3 fields "Number" fields?
  2. Should Total be updated any time Field A or Field B are updated?
  3. Or should Total be calculated only initially when the issue is created
  4. Should the user be able to override the value of Total for any reason ever?
  5. Should the user see the result of the calculation in real-time? e.g edit A and see the new total right away before saving? Or can it be calculated after saving and only appear on the view screen?
  6. There is an option to have "Total" be a scripted field rather than a regular Number field. In this case, it gets refreshed/calculated in real-time when the issue is accessed (but won't be available in the SQL db). Is that an appealing option?
  7. What should happen to Total when either A or B are left empty?

Hi there, Thank you for replying:

1. Yes

2. Yes

3. No

4. No preferably, but doesn't really matter.

5. Real time preferred.

6. Yes, that would be acceptable.

7. I need A and B to either default to 0 or be allowed to be empty.  In either case Total should total any field that has a value above 0. 

Wow, this is why I reach out for help.    @Peter-Dave Sheehan  

Because of your response to #5, my recommendation is to use a behavior script.

This way, the Total field will be visible on create/edit screens, but in a read-only fashion and calculate in real time as you change either Field A or FieldB.

The one CON to this option is that Field A and Field B will no longer be editable in-line. When you click on the pencil icon on one of those fields, the full edit screen will pop up.

Here is the script you will need:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def fieldA = getFieldByName('Field A')
def fieldB = getFieldByName('Field B')
def fieldTotal = getFieldByName('Total')

def valueA = (fieldA.value ?: 0) as Number
def valueB = (fieldB.value ?: 0) as Number

def totalValue = valueA + valueB


Now, go to Scriptrunner Behaviours

  1. Create a new behaviour configuration.
  2. Add mapping for your project (and issue type if applicable).
  3. Edit the behaviour config and add your 3 fields.
  4. On the total field, toggle the "writable" option to "readonly"
  5. On each of the other fields, click "add server-side script" and paste the script above in BOTH fields. Adjust the scripts to match the actual field names.

Here is what it looks like in my environment (I used 3 arbitrary number fields for example)

2022-09-01 10_57_07-Edit Behaviour_ test.png

You're incredible.  My favorite person in the world today!  I will get to work on this now.

What Peter is saying is absolutely correct and to consider.

Here is an exampel for a scripted field which probably is something you are looking for.

// Gesamtaufwand in PT
import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Logger

## Custom Field IDs ueberpueft am 07.05.2021
## Geplanter Aufwand FB = customfield_13666 - in allen Umgebungen gleiche ID
## Geplanter Aufwand ZI = customfield_13664 - in allen Umgebungen gleiche ID
## Aufwand sonstiges in PT = customfield_13681 - in allen Umgebungen gleiche ID
## Gesamtaufwand = customfield_15702

Integer[] arrCustomfieldId = [

int intSummeVerplant = 0;
for (int intCustomfieldId : arrCustomfieldId){
try {
intSummeVerplant += (Integer)issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_" + intCustomfieldId));
} catch(Exception e) {
log.warn("Catching Exception" + "\ne");

return (intSummeVerplant);

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events