Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner error with custom listener

Mike
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 28, 2020

My goal is to have a user create an issue within a specific JIRA project and based on they select within a custom field, will assign a component. The custom field is a database field.

 The component and names from within the custom field are not the same. The name of the database custom field is: Client name (17570)

I believe this can be accomplished using a script listener with Scriptrunner add-on. So far I have not been successful with the code below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
// a map with select list option to Component
def componentsMap = [
"Name 1" : "Component 1",
"Name 2" : "Component 2",
"Name 3" : "Component 2",
"Name 4" : "Component 1",
"Name 5" : "Component 1",
]
def issue = issue as MutableIssue
def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Client name")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption
// there is no value for the select list - therefore do nothing
if (! selectListValue)
{ return }
def componentManager = ComponentAccessor.projectComponentManager
def componentName = componentsMap.get(selectListValue.value)
def component = componentManager.findByComponentName(issue.projectObject.id, componentName)
if (component)
{ componentManager.updateIssueProjectComponents(issue, [component]) }

The error I am receiving:

2020-04-28 13:48:14,563 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-04-28 13:48:14,568 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Name 5' with class 'java.lang.String' to class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption'
	at Script522.run(Script522.groovy:299)



If anyone has recommendations, that would be appreciated. Thank you

1 answer

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 3, 2020

This is one of the wonderful things about coding that makes it look so inscrutable to non-coders.  It looks and sounds quite horrid, but isn't really.

The word "cast" is the giveaway.  Casting one thing to another means that you're changing the type of an informational object from one type to another, with the same "data" but different "information". 


As an example, you and I can see that "two", "2" and "II" are the same thing when written.  And without conscious consideration, probably pick up deux, dos, dois, zwei, bi and quite a lot of the other words in non-english languages.

Computers can't do that.  We need to tell them how to.

The problem in your code is that you have defined your components as "strings", but Jira needs to work with "options" when it comes to the components fields.  Options can't be just cast from a string.  You'll need to work out what "option" you want to use, probably by looking at the names for each option in your system.

Mike
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2020

Thanks Nic - I actually went a different route using JQL filters and Automation. This is helpful information, thanks

Suggest an answer

Log in or Sign up to answer