Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Is my script listener code correct?

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

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
Community showcase
Published in Jira Service Management

Next level incident management in Jira Service Management

Hello Atlassian Community! We’re thrilled to announce some exciting changes to incident management in Jira Service Management Cloud, in order to give your teams extra muscle during incidents, and ...

1,491 views 20 12
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you