Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Is my script listener code correct?

Sysad April 12, 2021

Hi Folks,

Following is my script listener to update a custom field called "Application" which is a multi-select field. On issue create event, if the issue summary contains the word JIRA, am trying to set the application field dropdown value to JIRA. Here is the code and the listener executes without an error BUT the field isn't getting updated in the issue create event, but when the issue summary is updated, then the listener throws an error.

any suggestions?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Application")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = event.issue as MutableIssue
def summary = issue.summary

if (issue.summary == "jira")
{
issue.setCustomFieldValue(cf, "JIRA")
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}



-------------------------------------------------------------------------------
Error log when issue summary is updated.


2021-04-12 09:03:19,368 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-04-12 09:03:19,369 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:728)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:681)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:667)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:217)
at com.atlassian.jira.issue.IssueManager$updateIssue$1.call(Unknown Source)
at Script101.run(Script101.groovy:14)

1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2021

It's this line:

issue.setCustomFieldValue(cf, "JIRA")

You can't just jam a string into a field unless it's a field that holds strings (short and long text).

You have two problems here - first, a select list holds options, not strings, and second, a multi-select is a collection of options, not a single option (even if there's only one value, it's still a collection.

What you should probably be doing is:

  • Read the current content of the field out as a collection
  • Look up an option for the field that is has the name "JIRA"
  • Add it to the current option collection
  • Post that collection back into the field

https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console is a good base for seeing how to handle all the different types of field.  Obviously, you can concentrate just on the multi-select bits.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events