Help with populating custom field using a groovy script with Post Function

I'm trying to populate a custom field (I have tried to populate a Number Field, Multi Select & Calculated number field) using the following groovy script that is being called from a "Script Post-Function" within the workflow and receive a javax.script.ScriptException, can anyone tell me what's wrong with my code?

{Code}

import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue

cfm = componentManager.getCustomFieldManager()
ch = new DefaultIssueChangeHolder();
Issue issue = issue
MeD = cfm.getCustomFieldObjects(issue).find {it.name == 'Month-End'}
cfMeD = issue.getCustomFieldValue(MeD)
Date now = new Date()
month = now.month + 1
log.warn "The Month value is: " +month
MeD.updateValue(null, issue, new ModifiedValue(MeD, month), ch)

{Code}

The error is:

2013-02-01 15:21:46,206 http-8081-12 WARN t16384 921x1999x1 rr89da 172.19.25.160 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The Month value is: 2
2013-02-01 15:21:46,206 http-8081-12 ERROR t16384 921x1999x1 rr89da 172.19.25.160 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
2013-02-01 15:21:46,206 http-8081-12 ERROR t16384 921x1999x1 rr89da 172.19.25.160 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:195)
at com.onresolve.jira.groovy.GroovyRunner.runFile(GroovyRunner.java:92)
at com.onresolve.jira.groovy.GroovyRunner.run(GroovyRunner.java:52)
at com.onresolve.jira.groovy.GroovyFunctionPlugin.execute(GroovyFunctionPlugin.java:35)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1050)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1446)
at com.opensymphony.workflow.AbstractWorkflow.doAction(AbstractWorkflow.java:564)
at com.atlassian.jira.workflow.OSWorkflowManager.doWorkflowActionInsideTxn(OSWorkflowManager.java:905)

Caused by: javax.script.ScriptException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
... 140 more


Caused by: java.lang.ClassCastException: java.lang.Integer 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.createValue(AbstractSingleFieldType.java:137)
at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:740)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:428)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:410)
at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at Script38.run(Script38.groovy:15)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)

2 answers

1 accepted

This widget could not be displayed.
Henning Tietgens Community Champion Feb 03, 2013

You should take care that the class of the custom field value (cfMeD) is the same as the new value. Try log.debug "${cfMeD.class}" after getCustomFieldValue.

If you are using this script as workflow postfunction before reindexing in JIRA 5.x you should take care to use the issue object to modify the issue. So you should use issue.setCustomFieldValue() instead of MeD.updateValue(). Otherwise you may get index problem.

if you use issue.setCustomFieldValue() before reindexing you don't need an issueManager.updateIssue() as JIRA itself is updating the issue.

Henning

Henning Tietgens Community Champion Feb 03, 2013

Please try issue.setCustomFieldValue(MeD, cal.get(Calendar.MONTH) as Double)

Henning: I tryied your suggestion as well and also received an error:

{Code}

import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue

cfm = componentManager.getCustomFieldManager()
ch = new DefaultIssueChangeHolder();
Issue issue = issue
MeD = cfm.getCustomFieldObjects(issue).find {it.name == 'Month-End'}
cfMeD = issue.getCustomFieldValue(MeD)
log.debug "${cfMeD.class}"
Calendar cal = Calendar.getInstance()
cal.add(Calendar.MONTH, 1)
log.warn "The Month value is: " +cal.get(Calendar.MONTH)
issue.setCustomFieldValue(MeD, 'Month-End')

{code}

2013-02-04 08:22:09,197 http-8081-5 WARN t16384 502x2503x1 33c582 172.19.25.160 /secure/CommentAssignIssue.jspa [innovalog.jmcf.fields.CalculatedNumberField] CalculatedNumberField: could not find formula in custom field description
2013-02-04 08:22:09,244 http-8081-5 WARN t16384 502x2503x1 33c582 172.19.25.160 /secure/CommentAssignIssue.jspa [onresolve.jira.groovy.GroovyRunner] The Month value is: 2
2013-02-04 08:22:09,260 http-8081-5 ERROR t16384 502x2503x1 33c582 172.19.25.160 /secure/CommentAssignIssue.jspa [atlassian.jira.workflow.OSWorkflowManager] Caught exception while attempting to perform action 211 from workflow 96178 on issue 'CC-4635'
java.lang.ClassCastException: java.lang.String 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:146)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:449)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:410)
at com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction.execute(GenerateChangeHistoryFunction.java:63)

Fantastic! That fixed my error, Thank you very much Henning

This widget could not be displayed.

Hi Jeff,

The problem is that in updateValue you pass ModifedValue object containing the Integer object month. A Date object is expected there. I assume you want to add one month to the current date and store that in field "Month-End".

IMO the best way to do date arithmetic is using the Calendar object. The Calendar object also can easily be converted in a Date object. So i suggest this:

import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue

cfm = componentManager.getCustomFieldManager()
ch = new DefaultIssueChangeHolder();
Issue issue = issue
MeD = cfm.getCustomFieldObjects(issue).find {it.name == 'Month-End'}
cfMeD = issue.getCustomFieldValue(MeD)

// get current date in cal
Calendar cal = Calendar.getInstance()
// add one month
cal.add(Calendar.MONTH, 1)

log.warn "The Month value is: " +cal.get(Calendar.MONTH)
//  ModifiedValue object must contain Date objects
MeD.updateValue(null, issue, new ModifiedValue(MeD, cal.getTime()), ch)

I haven't tried this on my own yet but it should help

regards,

Dieter

Thanks for your response Dieter, however I still received an error after using your code:

Caused by: javax.script.ScriptException: java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.Double
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

292 views 5 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you