Use Behaviors to sum two fields in the Story Points field

Hi,

Our situation is that we have a few developers who would like to break Story Points into 

Dev and QA Story Points. I would like to sum Dev and QA in the Story Points field using Behaviors. 

I only want Story Points to be the sum of Dev and QA if either value is not null (since some users do not want to split the Story Points).  

I put this code in both Dev and QA fields.

Only works if both Dev and QA are not null, then the sum is calculated in Story Points. However, I tried to make it so if one of the fields is not null and the other is null, Story Points is the value of the not null field. This does not work. 

def qa = getFieldById("customfield_11034")
def dev = getFieldById("customfield_11033")
def sum = getFieldById("customfield_10103")
def qavalue = qa.getValue() as Integer
def devvalue = dev.getValue() as Integer
if (qavalue != null && devvalue == null) {
    sum.setFormValue(qavalue);
 }
else if (devvalue != null && qavalue == null) {
    sum.setFormValue(devvalue);
 }
else if (qavalue != null && devvalue != null) {
    return sum.setFormValue(devvalue + qavalue);
 }
else return null;

2 answers

For both of them can you not just do:

def qavalue = qa.getValue() as Integer ?: 0

Then they will both non-null, and you can just add them and avoid the null-checking.

Or use what Nic said but you do need the setFormValue.

Even neater than my code.

Thanks guys,

I put this 

def qa = getFieldById("customfield_11034")
def dev = getFieldById("customfield_11033")
def sum = getFieldById("customfield_10103")
def qavalue = qa.getValue() as Integer ?: 0
def devvalue = dev.getValue() as Integer ?: 0  
return sum.setFormValue(qavalue + devvalue)

in the Validation Script for both QA and Dev fields. The null value doesn't seem to be setting to zero. 

Maybe I am doing something wrong.

You don't need to return it but should not make a difference.

Can you describe when it should be set to zero? 

Your question wasn't really clear, is that you want the sum to be zero if either field is zero?

If so change the last line to

if (qavalue && devvalue) {
    sum.setFormValue(qavalue + devvalue)
}
else {
    sum.setFormValue(0)
}

In our situation, we only want the sum (Story Points) to be set if:

  1. qavalue and devvalue are not null 
    1. sum = qavvalue + devvalue
  2. qavalue is not null and devvalue is null
    1. sum = qavvalue
  3. devvalue is not null and qavalue is null 
    1. sum = devvalue

If both, qavalue and devvalue are null, then sum value should not be set based on these fields. 

Apologies for my incorrect "return" statements earlier.

I think you're very close, but need an empty field instead of a 0 when both source fields are empty.  So:

def qavalue = qa.getValue() as Integer ?: 0
def devvalue = dev.getValue() as Integer ?: 0 
if (qavalue || devvalue) {
  sum.setFormValue(qavalue + devvalue)
}
else {
  sum.setFormValue()
}

However... I'm not sure of my coding here - that second "setFormValue" is trying to say "set field to empty", but I don't know if () is the right way to do it - it might be ("null") or possibly some other function call like sum.clearFormValue()

(I just did a ScriptRunner introduction course - there wasn't much coding and four of the modules were nothing new, but the fifth module was on "Behaviours" and confirmed that I really don't know how to write for that)

IIRC: 

sum.setFormValue("")

I missed the training course unfortunately sad

I'm trying to do something similar and am VERY new to Groovy (also, don't have a dev background but I'm learning...by trial and error).

 

We have Behaviours and I want to sum the value of two fields in a third filed so I assumed adding a script to those fields would be easiest. I want the scripts to run when any of the two "input fields" are updated. Here's my feeble attempt based on online research/trying...

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;
import com.atlassian.jira.issue.MutableIssue;

int bizvalue = customFieldManager.getCustomFieldObjectByName( "User/Biz Value") as Integer;
int time = customFieldManager.getCustomFieldObjectByName( "Time Criticality" ) as Integer;
int costofdelay = customFieldManager.getCustomFieldObjectByName( "Cost of Delay" ) as Integer;

cost = sum.setFormValue(bizvalue + time)
setCustomFieldValue(costofdelay, cost);

I keep getting a few errors at the end of the console when I use this script and I'm kind of at a roadblock with figuring out what to do...help? 

0 votes

From your first if, try:

def result = 0 
if (qavalue) { result += qavalue }
if (devalue) { result += devvalue }
if ( result != 0 ) {
  return result
} 
else return null

 

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Jira

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,122 views 0 8
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you