Forums

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

ScriptRunner - Groovy: java.lang.NumberFormatException

Cynthia Gallardo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 28, 2019

I’m trying to create a script runner script for a new scripted field that will calculate Weighted Shortest Job First (WSJF) which is a prioritization model used by SAFe. The script will get values from numeric fields and calculate a value based on those values – specifically ((Business Value + Time Criticality + Risk Oppty Value)/ Job Size). I’ve leveraged the work done on the following question https://community.atlassian.com/t5/Jira-questions/ScriptRunner-Groovy-getting-fields-and-adding-them-to-store-into/qaq-p/745252 but know something has to change to account for numeric values.  Currently getting java.lang.NumberFormatException: For input string: "5.0" error. when I attempt to preview an issue with values. Any guidance is appreciated.

 

import com.atlassian.jira.ComponentAccessor

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.ComponentManager

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

 

 

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField bvField = customFieldManager.getCustomFieldObject("customfield_10003");

CustomField tcField = customFieldManager.getCustomFieldObject("customfield_16149");

CustomField rrField = customFieldManager.getCustomFieldObject("customfield_16150");

CustomField jobSizeField = customFieldManager.getCustomFieldObject("customfield_16148");

 

String bvValue = issue.getCustomFieldValue(bvField).toString();

String tcValue = issue.getCustomFieldValue(tcField).toString();

String rrValue = issue.getCustomFieldValue(rrField).toString();

String jobSizeValue = issue.getCustomFieldValue(jobSizeField).toString();

 

int jobSizeInt = Integer.parseInt(jobSizeValue);

int rrInt = Integer.parseInt(rrValue);

int tcInt = Integer.parseInt(tcValue);

int bvInt = Integer.parseInt(bvValue);

 

def total = (bvInt + tcInt + rrInt) / jobSizeInt

 

 

return total

1 answer

1 accepted

1 vote
Answer accepted
Tuncay Senturk _Snapbytes_
Community Champion
March 1, 2019

Jira number fields return Double type, so you may need to do a conversion as below

int rrInt = ((Double)rrValue).intValue()

 But better option is as below

Double rrValue = (Double)issue.getCustomFieldValue(rrField);

int rrInt = rrValue.intValue()
Cynthia Gallardo
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 1, 2019

Thank you! That worked out perfectly! Updated script:

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
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

 

 

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField bvField = customFieldManager.getCustomFieldObject("customfield_10003");
CustomField tcField = customFieldManager.getCustomFieldObject("customfield_16149");
CustomField rrField = customFieldManager.getCustomFieldObject("customfield_16150");
CustomField jobSizeField = customFieldManager.getCustomFieldObject("customfield_16148");


Double bvValue = (Double)issue.getCustomFieldValue(bvField);
Double tcValue = (Double)issue.getCustomFieldValue(tcField);
Double rrValue = (Double)issue.getCustomFieldValue(rrField);
Double jobSizeValue = (Double)issue.getCustomFieldValue(jobSizeField);

 

int jobSizeInt = jobSizeValue.intValue();
int rrInt = rrValue.intValue();
int tcInt = tcValue.intValue();
int bvInt = bvValue.intValue();

 

def total = (bvInt + tcInt + rrInt) / jobSizeInt

return total

Tuncay Senturk _Snapbytes_
Community Champion
March 1, 2019

Hello @Cynthia Gallardo 

I'm glad that it works. Please accept answer if it helped. 

Thanks

Suggest an answer

Log in or Sign up to answer