Custom field calculated from other drop-down-fields

TIS Admin August 9, 2019

Hello Atlassian Community,

 

I am trying to get a custom field that is based on a result from 5 more custom fields values (Drop-down Single). In the past I did this with Power Custom Fields SIL but now it's paid and we don't want to spend 2000$ only for this field. We have Adaptavist Scriptrunner license and I am sure we can do it with it? 
So I have 5 custom fields with options: High, Medium, Low. Each of them represent a number (3,2,1). 
The goal is that my custom field will show a value based on the SUM of these fields.

This should be really simple if you look at my code: 

def damage = getCustomFieldValue("Damage")
def reproducibility = getCustomFieldValue("Reproducibility")
def exploitability = getCustomFieldValue("Exploitability")
def affectedUsers = getCustomFieldValue("Affected users")
def discoverability = getCustomFieldValue("Discoverability")

def damagerating;
def reproducibilityrating;
def exploitabilityrating;
def affectedUsersrating;
def discoverabilityrating;
def scoreTotal;

if ( damage == "Low" ){
damagerating = 1;
} else if ( damage == "Medium" ){
damagerating = 2;
} else if ( damage == "High" ){
damagerating = 3;
}

if ( reproducibility == "Low" ){
reproducibilityrating = 1;
} else if ( reproducibility == "Medium" ){
reproducibilityrating = 2;
} else if ( reproducibility == "High" ){
reproducibilityrating = 3;
}

if ( exploitability == "Low" ){
exploitabilityrating = 1;
} else if ( exploitability == "Medium" ){
exploitabilityrating = 2;
} else if ( exploitability == "High" ){
exploitabilityrating = 3;
}

if ( affectedUsers == "Low" ){
affectedUsersrating = 1;
} else if ( affectedUsers == "Medium" ){
affectedUsersrating = 2;
} else if ( affectedUsers == "High" ){
affectedUsersrating = 3;
}

if ( discoverability == "Low" ){
discoverabilityrating = 1;
} else if ( discoverability == "Medium" ){
discoverabilityrating = 2;
} else if ( discoverability == "High" ){
discoverabilityrating = 3;
}


scoreTotal = (damagerating + reproducibilityrating + exploitabilityrating + affectedUsersrating + discoverabilityrating)

if (scoreTotal >= 10) {
return "High";
}

if (scoreTotal >= 5) {
return "Medium";
}

if (scoreTotal < 5) {
return "Low";
}
The field is not even show in the Screen where it's configured. When I try the Preview I get"
Result: null
Log: 
2019-08-09 09:35:49,144 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2019-08-09 09:35:49,144 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [Damage] at Script163.run(Script163.groovy:1)


Highly appreciate any help :) 

1 answer

1 accepted

1 vote
Answer accepted
Ilya Turov
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.
August 9, 2019

rewrite the first "paragraph" of your code like this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def damageField = customFieldManager.getCustomFieldObjectsByName("Damage")[0]
def damage = issue.getCustomFieldValue(damageField)?.value

I think you got the idea and can apply it to other fields. Basically, you need to get the object representing your custom field first, and then get it's value from the issue.

TIS Admin August 9, 2019

Hi Ilya,  thank you first! So... there is a progress! 

I am now able to get the values from the custom fields using this code: 

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

def customFieldManager = ComponentAccessor.customFieldManager

def damageField = customFieldManager.getCustomFieldObjectsByName("Damage")[0]
def damage = issue.getCustomFieldValue(damageField)
Now the problem is with this part that converts the value from string to number: 

if ( damage == "Low" ){
damagerating = 1;
} else if ( damage == "Medium" ){
damagerating = 2;
} else if ( damage == "High" ){
damagerating = 3;
}

 Now it returns proper result for "damage', but for damagerating it returns null

Do you have idea?

Ilya Turov
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.
August 9, 2019

if it's single select type custom field, you should use 

issue.getCustomFieldValue(damageField)?.value

because just 

issue.getCustomFieldValue(damageField)

gives you Option, not the String, which you want to compare it to

Like TIS Admin likes this
TIS Admin August 9, 2019

This is what I get when I put "?.value"

jiravalue.png

 

Also it really returns the proper value of this field for the previewed issue.

Ilya Turov
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.
August 9, 2019

because we are using dynamic typing here, it doesnt instantly knows that the customFieldValue we are getting here is Option type (which has the value property)

I'd say, most times you can ignore static type checking errors if you have idea what you are doing (like in this case, Im sure that "damage" field is select list, so it's value gonna have a value property and i can safely ask for it)

Like TIS Admin likes this
TIS Admin August 9, 2019

Thank you very much! It works now regardless the checking :) 

I wish you an awesome weekend, you saved me! 

neeta jain August 26, 2020

Hi TIS Admin,

Please check my below script , I am getting an error at the declaration:

I want a calculated value from 2 custom fields, so created a script field for it and written down the below script:

Please suggest your inpits on it, as its not working.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.Issue

 

Issue mainIssue = issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def riskimpact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18484".getValue())) as Integer

 

def riskoccprobablity = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18483".getValue())) as Integer

 

def risklevel = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_23091".getValue())) as Integer

 

if ( ( riskimpact * riskoccprobablity) == 0)

{

    return risklevel == 0

}

else if  ( ( riskimpact * riskoccprobablity) < 4)

{

    return risklevel == 1

}

else if  ( ( riskimpact * riskoccprobablity)  < 8 )

{

    return risklevel == 2

}  

else

{

    return risklevel == 3

}

Suggest an answer

Log in or Sign up to answer