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)
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
Please try issue.setCustomFieldValue(MeD, cal.get(Calendar.MONTH) as Double)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.
Get Certified! ✍️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.