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

1 vote

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

0 vote

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

736 views 3 11
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