We have a working script to calculate a series of 13 percentages that are used to populate 13 numeric fields on a transition post function; however, we are having difficulties in rounding the values to achieve an integer resultant. The working script follows:
----------------------------------------------
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
Issue issue = issue;
def fieldList = [
[Numer:'LIFE Enrolled Lives',Denom:'LIFE Eligible Lives',Reslt:'LIFE Participation Percentage'],
[Numer:'VTL Enrolled Lives',Denom:'VTL Eligible Lives',Reslt:'VTL Participation Percentage'],
[Numer:'STD Enrolled Lives',Denom:'STD Eligible Lives',Reslt:'STD Participation Percentage'],
[Numer:'PCVSTD Enrolled Lives',Denom:'PCVSTD Eligible Lives',Reslt:'PCVSTD Participation Percentage'],
[Numer:'LTD Enrolled Lives',Denom:'LTD Eligible Lives',Reslt:'LTD Participation Percentage'],
[Numer:'VLTD Enrolled Lives',Denom:'VLTD Eligible Lives',Reslt:'VLTD Participation Percentage'],
[Numer:'DENTAL Enrolled Lives',Denom:'DENTAL Eligible Lives',Reslt:'DENTAL Participation Percentage'],
[Numer:'VDEN Enrolled Lives',Denom:'VDEN Eligible Lives',Reslt:'VDEN Participation Percentage'],
[Numer:'CI Enrolled Lives',Denom:'CI Eligible Lives',Reslt:'CI Participation Percentage'],
[Numer:'VCI Enrolled Lives',Denom:'VCI Eligible Lives',Reslt:'VCI Participation Percentage'],
[Numer:'ACC Enrolled Lives',Denom:'ACC Eligible Lives',Reslt:'ACC Participation Percentage'],
[Numer:'VACC Enrolled Lives',Denom:'VACC Eligible Lives',Reslt:'VACC Participation Percentage'],
[Numer:'Other Enrolled Lives',Denom:'Other Eligible Lives',Reslt:'Other Participation Percentage']
]
fieldList.each { val ->
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtNumerator = customFieldManager.getCustomFieldObjects(issue).find
{it.name == val.Numer}
def tgtDenominator = customFieldManager.getCustomFieldObjects(issue).find
{it.name == val.Denom}
def tgtResult = customFieldManager.getCustomFieldObjects(issue).find
{it.name == val.Reslt}
def numerator = issue.getCustomFieldValue(tgtNumerator)
def denominator = issue.getCustomFieldValue(tgtDenominator)
def result = issue.getCustomFieldValue(tgtResult)
def changeHolder = new DefaultIssueChangeHolder() //Do not need to recreate
if ((numerator && numerator > -1) && (denominator && denominator > 0 ))
{ tgtResult.updateValue(null, issue, new ModifiedValue(result, (numerator/denominator)*100),changeHolder) }
else
{ tgtResult.updateValue(null, issue, new ModifiedValue(result, null),changeHolder) }
}
-------------------------------------
Our attempts to use Math.round for the rounding are successful when using the Groovy Web Console but have not been successful when using your ScriptRunner plugin. See sample error message below:
2016-03-23 08:04:00,376 http-bio-8530-exec-9 ERROR req86806 484x68791x1 q47b5h 10.9.97.104,10.8.123.95 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: VERA-15, actionId: 181, file: <inline script> java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:40) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:170) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:526) at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:487) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script97$_run_closure1.doCall(Script97.groovy:40) at Script97.run(Script97.groovy:24)
Please provide needed guidance/tips.
Many thanks for your efforts.
Could you format the code using the {code} macro, its difficult to read.
It looks like your custom field is returning a long where Math.round is expecting a double. You need to convert it to a double like so if you want to use Math.round.
Long l = new Long(15552451L) double d = l.doubleValue()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.