Calculate a field impact*likelyhood-risk

I know my way around Jira, but my scripting skills are non-exesisting.

We use Scriptrunner and i created a custom scripted field, which is read only and should be filled automatically.

I would love this field to be filled with the calculation of 2 numeric fields

Field one = Risk Likelihood

Field two= Risk Impact

Both fields should then calculate simply to the Risk value. in a way of Likelyhood*Impact=Risk

 

This is where I'm at now, but nothing happens.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def likelyhood = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Likelyhood"))
def impact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Impact")
if (likelyhood) {
 return likelyhood * impact)
}
else {
 return null
}

2 answers

1 votes
Alexey Matveev Community Champion Dec 04, 2017

Can you compile your code. You have wrong parenthis closing. Your code should be like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def likelyhood = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Likelyhood"))
def impact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Impact"))
if (likelyhood) {
 return likelyhood * impact
}
else {
 return null
}
0 votes

Hi all - I'm in the same boat as Judith (scripting skills are nearly non-existent).  I am trying to create a nearly identical field that is described again.  I continue to get an error, and I can't figure out why.  Thanks in advance!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def likelihood = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Risk Likelihood"))
def impact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Risk Impact"))
if (likelihood) {
return likelihood * impact
}
else {
return null
}

 
When I run a preview on an issue that has number values in both fields, I get this error:


groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.customfields.option.LazyLoadedOption.multiply() is applicable for argument types: (com.atlassian.jira.issue.customfields.option.LazyLoadedOption) values: [5]


Any help is appreciated!

Your code is mostly fine, but you're missing a bit of java/groovy knowledge that is not obvious or intuitive to new-ish coders.

issue.getCustomFieldValue does what it says on the tin - it gets the value of the field for that issue.  However, that value can vary in type, and type determines what you can do with that value.  In Jira, there are several possible types of data a custom field can hold.  You can probably guess that text fields return strings, numeric fields give you numbers, dates and times give you a timestamp type data and so-on.

The problem with your code is that your fields are select lists.  Their values are a Jira type called "options", which are structures peculiar to Jira, and, like strings and dates, have no way to multiply together because it's nonsense.  In the same way that multiplying strings makes no sense (for example, 2 * 3 = 6, but what do you get from "Penguin * Aspidistra"?)

Your code needs to interpret the options in a way that gives you numbers that you can multiply together.

I don't know what you have got for options in these fields.  It sounds like you might have put in numbers, in which case you can do something like

def impactnumber = Integer.parseInt( impact.getValue() )

But if you've used more human representations like "high, medium, low", you'll need something more interpretive, like

if ( "high".equals (impact.getValue()) ) { impactnumber = 5 }

Thanks @Nic Brough [Adaptavist], your explanation makes perfect sense.  And that was along the lines I was thinking.  I had attempted a few methods to convert the value to an int, but was unsuccessful.  

I tried the method you recommended, and it worked!  Thank you again

 

Jeremy

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,816 views 11 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot