Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Error in scripted field code - Script Runner

Christophe De Roeck February 25, 2019

Hi,

In our journey towards the implementation of SAFe in our organization, I want to implement some SAFe parameters in our JIRA environment. One of these is the Weighted Shortest Job First (WSJF). A scripted field (using the Script Runner plug-in) is used to calculate the WSJF value based on a simple formula (four numeric values entered in four custom fields).

I have no Java or Groovy experience, so the code I came up with is just based on examples I found elsewhere:

The code below works fine (WSJF value is as it should be), but I get an 'error' (see screenshot) on the line with the actual formula (last if-statement). I don't want to implement the field with this error, so any suggestion is appreciated. Thx

import com.atlassian.jira.component.ComponentAccessor

def BUS_VAL_FIELD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Business_Value")
def BUS_VAL= issue.getCustomFieldValue(BUS_VAL_FIELD)
def TIME_CRIT_FIELD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Time_Criticality")
def TIME_CRIT= issue.getCustomFieldValue(TIME_CRIT_FIELD)
def RISK_REDUC_FIELD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Risk_Reduction")
def RISK_REDUC= issue.getCustomFieldValue(RISK_REDUC_FIELD)
def JOB_DUR_FIELD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Job_Duration")
def JOB_DUR= issue.getCustomFieldValue(JOB_DUR_FIELD)

if (BUS_VAL) {
if(TIME_CRIT) {
if(RISK_REDUC) {
if(JOB_DUR) {return (BUS_VAL + TIME_CRIT + RISK_REDUC)/JOB_DUR}
else {return null}
}
else {return null}
}
else {return null}
}
else {return null}

error.jpg

 

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
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.
February 26, 2019

The problem is that getCustomFieldValue could return any number of types of object, depending on what the data is (a number type object for numbers and estimates, a date/time stamp for dates and date/times, a string for text fields, options, versions, users etc)

So when you're trying to add the fields together, it fails.  Not because the data is wrong - you almost certainly know the four fields you are working with contain numbers, but the code does not know that it's always going to get numbers, just an "object".  Generic objects don't support addition off the shelf (imagine trying to do the arithmetic on " 42 + 3.4 + the sound of a cat's footfall + 7 + coffee = ")

The short way to fix this is to define the four field values explicitly, using "double" or "long" instead of "def".  "Def" is short for groovy's "define", which creates an untyped object that will then accept any object.  But if you say "double", you are telling the code it will always get a double back from the call, and doubles can be added together.  (Of course, this means if the field is null, or not a number, you've got more errors, but that's another essay for later)

Christophe De Roeck February 26, 2019

Nic,

Thank you for the info and explanation

Changing 4 lines to

def BUS_VAL= (double) issue.getCustomFieldValue(BUS_VAL_FIELD)

did the tric.

Now indeed, I still need to figure out how to cope with empty fields

Christophe

Suggest an answer

Log in or Sign up to answer