Forums

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

Compute output of single select fields via ScriptRunner

Joel Batac
Contributor
May 20, 2022

Hi,

 I'm trying to compute the output of 4 single select type fields via ScriptRunner but not getting any success. Any advice?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

def CFM = ComponentAccessor.getCustomFieldManager()
def risk = CFM.getCustomFieldObject(12345)
def impact = CFM.getCustomFieldObject(23456)
def confidence = CFM.getCustomFieldObject(34567)
def effort = CFM.getCustomFieldObject(45678)


def riskIntVal = risk.getValue(issue) as Integer
def impactIntVal = impact.getValue(issue) as Integer
def effortIntVal = effort.getValue(issue) as Integer
def confidencetIntVal = confidence.getValue(issue) as Integer

//return riskIntVal
//return impactIntVal
//return effortIntVal
//return confidencetIntVal

def riskScore = (riskIntVal * impactIntVal * effortIntVal * confidencetIntVal )
return riskScore.toString()
//log.warn(riskScore.getValue(issue));

 

 

ERROR CODE:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '4' with class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' to class 'java.lang.Integer' at Script815.run(Script815.groovy:12)

3 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 20, 2022

Hi @Joel Batac

The error appears to be coming from your calculation.

For your calculation line, ensure that you convert the result to Integer as well, like:-

def riskScore = (riskIntVal * impactIntVal * effortIntVal * confidencetIntVal ) as Integer

Finally, you do not need to use the return keyword for your result. Just use:-

riskScore.toString()

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Joel Batac
Contributor
May 21, 2022

Hi Ram,

 

 I did what you've suggested but got the same error code.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 22, 2022

Hi @Joel Batac

If you want your code to work without any exceptions, you should do something like this:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def number1 = customFieldManager.getCustomFieldObjectsByName('Number1').first()
def number2 = customFieldManager.getCustomFieldObjectsByName('Number2').first()
def number3 = customFieldManager.getCustomFieldObjectsByName('Number3').first()
def number4 = customFieldManager.getCustomFieldObjectsByName('Number4').first()

def number1Value = issue.getCustomFieldValue(number1) as String
def number2Value = issue.getCustomFieldValue(number2) as String
def number3Value = issue.getCustomFieldValue(number3) as String
def number4Value = issue.getCustomFieldValue(number4) as String

if (null in [number1Value, number2Value, number3Value, number4Value]) {
0
} else {
def number1Parse = Integer.parseInt(number1Value)
def number2Parse = Integer.parseInt(number2Value)
def number3Parse = Integer.parseInt(number3Value)
def number4Parse = Integer.parseInt(number4Value)
(number1Parse * number2Parse * number3Parse * number4Parse) as Integer
}

Please note that this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

If you do not use the null check, and if any of the lists are left unselected, i.e. set to None, it will cause a NumberFormatException

Below is a screenshot of the updated configuration:-

scripted_field-1.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Joel Batac
Contributor
May 23, 2022

@Ram Kumar Aravindakshan _Adaptavist_ Thanks, this worked. Followup question, is it possible to assign the result of the calculation to a Custom Number field?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 28, 2022

Hi @Joel Batac 

In your last comment, you asked:-

Followup question, is it possible to assign the result of the calculation to a Custom Number field?

To answer your question, yes, it is possible.

You can do it with a Listener using the IssueCreated Event and IssueUpdated Event.

With the Listener, you can either copy the value Scripted Field or reuse the same code in Scripted Field, with a slight modification, i.e. for the IssueCreated and IssueUpdated, you will need to use event.issue to invoke the issue variable.

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

0 votes
Andrea Pannitti
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.
May 21, 2022

Hi @Joel Batac ,

you could change the rows:

def riskIntVal = risk.getValue(issue) as Integer
def impactIntVal = impact.getValue(issue) as Integer
def effortIntVal = effort.getValue(issue) as Integer
def confidencetIntVal = confidence.getValue(issue) as Integer

with the followings:

def riskIntVal = Integer.valueOf(risk.getValue(issue).toString())
def impactIntVal = Integer.valueOf(impact.getValue(issue).toString())
def effortIntVal = Integer.valueOf(effort.getValue(issue).toString())
def confidencetIntVal = Integer.valueOf(confidence.getValue(issue).toString())
0 votes
Andrea Pannitti
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.
May 20, 2022

Hi @Joel Batac ,

It seems that an option value cannot to be casted to integer.

Joel Batac
Contributor
May 21, 2022

what will be my other options? have them as Number field type?

Suggest an answer

Log in or Sign up to answer