I wrote a script Post-Function [ScriptRunner] in a transition to set a field "Commission Date" to today's date if the field is empty. I'm getting errors, but I'm obviously missing something. I 'think' the types and methods are declared correctly.
Thanks in advance for your help!
--Michelle
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import java.sql.Timestamp
// get current issue
Issue thisissue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// get today's date
def today = new java.sql.Timestamp(new Date().getTime())
def cfFuncDate = customFieldManager.getCustomFieldObjectsByName("Commission Date")
def funcDate = issue.getCustomFieldValue(cfFuncDate)
if (!funcDate) // if this field is empty, then the current date is set for this field
issue.setCustomFieldValue(cfFuncDate, today)
In the workflow editor line 15 errors;
[Static type checking] - Cannot find matching method com.atlassian.jira.issue.MutableIssue#getCustomFieldValue(java.util.Collect). Please check if the declared type is correct and if the method exists. @ line 15, column 16.
In the workflow editor line 17 errors;
[Static type checking] - Cannot find matching method com.atlassian.jira.issue.MutableIssue#setCustomFieldValue(java.util.Collect, java.sql.Timestamp). Please check if the declared type is correct and if the method exists. @ line 17, column 5.
In the logs the error message is;
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setCustomFieldValue() is applicable for argument types: (com.google.common.collect.SingletonImmutableList, java.sql.Timestamp) values: [[Comm
ission Date], 2019-04-15 12:48:41.584]
Possible solutions: setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object), getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField)
at Script28.run(Script28.groovy:17)
getCustomFieldValue() expects you to pass in a singular custom field object. You're passing in a collection of custom field objects, because you're using getCustomFieldObjectsByName() to get the field. That seems to be causing the error.
Use
def cfFuncDate = customFieldManager.getCustomFieldObject("Commission Date")
to get a single custom field object
Thanks for the quick response. That definitely fixed the reported errors. I replaced my assignment with yours and it removed the compiled errors on the screen, but when I ran a test case I still received an error on the next line. Thanks again for getting me past those initial errors.
2019-04-15 23:39:56,618 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-04-15 23:39:56,618 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: HDW-7876, actionId: 201, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$3.call(Unknown Source) at Script10.run(Script10.groovy:15)
def funcDate = issue.getCustomFieldValue(cfFuncDate)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Michelle,
No problem. From the new error, it sounds like the custom field object is null. Are you sure the field is called "Commission Date" and not something like "Commission_Date"?
I'd put in an assert statement to quickly test whether or not the custom field object is null. Check the run history for the post-function whenever you test it again:
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// get today's date
def today = new Timestamp(new Date().getTime())
def cfFuncDate = customFieldManager.getCustomFieldObject("Commission Date")
assert cfFuncDate != null
def funcDate = issue.getCustomFieldValue(cfFuncDate)
if (!funcDate) { // if this field is empty, then the current date is set for this field
issue.setCustomFieldValue(cfFuncDate, today)
}
While your script is pretty much correctly written, you might also like our new free Script Library site that we've launched. We have tons of example scripts, including an example of how to set a custom field in a workflow transition. See here: https://library.adaptavist.com/entity/change-the-value-of-a-custom-field-in-a-post-function
Regards,
Josh
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.