Calculate a field impact*likelyhood-risk

Judith Leupen December 4, 2017

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 vote
Alexey Matveev
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.
December 4, 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
Jeremy Barrett January 6, 2018

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!

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.
January 6, 2018

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 }

Jeremy Barrett January 7, 2018

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 Sign up to answer