Exception when checking value of string in ScriptRunner

Amir Katz (Outseer)
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 9, 2018

I'm trying to add a post-function on create issue (which will assign the issue to a role), but only if a condition is true.

The custom field type is 'select list, single choice', so I assume the value is a string.

I added the code below for the condition, but when I click on the 'Test Groovy script' I get an error.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.opensymphony.workflow.InvalidInputException

def issue = issue as MutableIssue
final myField = "Product Name"

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(myField)
def cfValue = issue.getCustomFieldValue(cf)

return (cfValue.startsWith("AA"))

 

Message:
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.customfields.option.LazyLoadedOption.startsWith() is applicable for argument types: (java.lang.String) values: [AA]

 I tried to replace the literal "AA" with a variable (final foo = "AA") and use it instead, but I get the same error.

Thanks

1 answer

1 accepted

1 vote
Answer accepted
David Fischer
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 9, 2018

Hi Amir,

are you sure you are using a Script Runner post-function? It looks like you might be using an Assign to Role Member post-function from JMWE instead.

Amir Katz (Outseer)
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 9, 2018

Thanks, David, you are correct, it's indeed a JMWE post-function.

But, my understanding is that the condition is a pure groovy code. The UI says, "Only execute this post-function if the following Groovy expression returns true".

Just as a side note, unrelated to the problem, we also have the JEP plugin which offers this post-function: "JEP - Assign issue to a role, according to a field of issue".

I will try it and report back here, but I'm still curious why my groovy code fails.

David Fischer
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 9, 2018

The Condition is indeed a Groovy script, but you are also using an API. You can, as in your case, use the standard Jira API (as you would in ScriptRunner), but then the mistake in your script is that the getCustomFieldValue method returns an Option object, which in turn has a getValue() method that returns the String representation of that option.

Or you can use JMWE's simplified API:

issue.get("Product Name")?.startsWith("AA")
Like Amir Katz (Outseer) likes this
Amir Katz (Outseer)
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 10, 2018

Hey David, thanks! I added the getValue() and it works:

def cfValue = issue.getCustomFieldValue(cf).getValue()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events