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
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:
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);
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. @PD 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.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours fieldBehavioursdef fieldA = getFieldByName('Field A')def fieldB = getFieldByName('Field B')def fieldTotal = getFieldByName('Total')def valueA = (fieldA.value ?: 0) as Numberdef valueB = (fieldB.value ?: 0) as Numberdef totalValue = valueA + valueBfieldTotal.setFormValue(totalValue)
Now, go to Scriptrunner Behaviours
Here is what it looks like in my environment (I used 3 arbitrary number fields for example)
You're incredible. My favorite person in the world today! I will get to work on this now.
It looks like you're new here. Sign in or register to get started.