Use Behaviors to sum two fields in the Story Points field

Angela Tran March 27, 2017

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

1 vote
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2017

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2017

Even neater than my code.

Angela Tran March 28, 2017

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.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2017

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?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2017

If so change the last line to

if (qavalue && devvalue) {
    sum.setFormValue(qavalue + devvalue)
}
else {
    sum.setFormValue(0)
}
Angela Tran March 28, 2017

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. 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2017

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)

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 28, 2017

IIRC: 

sum.setFormValue("")

I missed the training course unfortunately sad

David Phillips July 27, 2018

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);
David Phillips July 27, 2018

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
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2017

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