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)
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
Hi Ram,
I did what you've suggested but got the same error code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
I hope this helps to answer your question. :)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_ Thanks, this worked. Followup question, is it possible to assign the result of the calculation to a Custom Number field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Joel Batac ,
It seems that an option value cannot to be casted to integer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
what will be my other options? have them as Number field type?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.