Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
4,556,415
Community Members
 
Community Events
184
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:  https://community.atlassian.com/t5/Adaptavist-questions/SUM-the-values-of-2-Number-Custom-Fields-in-a-3rd-Custom/qaq-p/1248782#U2122588

2 answers

2 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Aug 31, 2022

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  

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 01, 2022

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

fieldTotal.setFormValue(totalValue)

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 = [
13666,
13664,
13681
]

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
TAGS
AUG Leaders

Atlassian Community Events