Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

ThinkTilt is joining the Atlassian Family!

This morning, Atlassian announced the acquisition of ThinkTilt , the maker of ProForma, a no-code/low code form builder with 700+ customers worldwide. ThinkTilt helps IT empower any team in their or...

856 views 30 33
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