Scriptrunner post-function: Check if customfield has certain value

Marc Scholte March 29, 2023

Hey all,

 

I have a post-function that creates a sub-task whenever a specific sub-task resolves. I wish to add a condition based on a custom field, but I am not succeeding.

def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_id")
def cfv1 = issue.getCustomFieldValue(cf1)

if (issue.isSubTask() &&
issue.summary.contains("text") &&
cfv1 == "Yes")

Above doesn't result in errors, but neither results in a created sub-task when the value is Yes. Logs are also empty.

I've also tried:

issue.customfield_11301 == "Yes"
[Static type checking] - No such property: customfield_11301 for class: com.atlassian.jira.issue.MutableIssue

What am I missing here?

Thanks in advance.

1 answer

1 accepted

2 votes
Answer accepted
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.
March 29, 2023

Hi Marc,

My first question is probably not the problem - you've got "customfield_id" in the "def cf1" line.  Do you mean that?  If customfield_id is a variable you've set earlier, it shouldn't be in quotes.  If it's a direct name, then shouldn't it be "customfield_11301"?

If that's not the problem, then the next question is "what type of field is 11301"?  If it's a string, and the field contains exactly "Yes", then your code should be working.  But if it's a select list then the content you get back from getCustomFieldValue() won't be a string - it will be an option object.

If it is an option-type field then you can use cfv1.getName() to get the display name of the option (which will be the "Yes" you can compare with another string)

(Oh, and if it's a multi-select type field, you'll get an array of options)

Marc Scholte March 29, 2023

Haha that's just me failing at censoring. It is actually

def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11301") 

 Like you said.

The field is a Select List (single choice).

If I change it to cfv1.getName() I get the following error:

Cannot find matching method com.atlassian.jira.issue.customfields.option.Option#getName(java.lang.String). Please check if the declared type is correct and if the method exists.

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.
March 29, 2023

:-)

We can't tell much from customfield_12345 - it's vanishingly rare that two systems will use the same id for a user-created custom field that have the same type and name!  (Except when you clone a system, which would be within your organisation and for doing development, testing, migration or upgrades)

Anyway, that means the problem is the fetch of the data from it.  And I've mislead you.

getName() works in a lot of places in Jira, but when it does not, getValue() will.  I can never remember which one works where!

Apologies for that!

Like Marc Scholte likes this
Marc Scholte March 29, 2023

Thanks a lot, final result which is working:

def cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_id")
def cfv1 = issue.getCustomFieldValue(cf1)

if (issue.isSubTask()
&& issue.summary.contains("text")
&& cfv1.getValue() == "Yes"

Like Nic Brough -Adaptavist- likes this
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.
March 31, 2023

Great!  Thanks for letting us know you got it!

Suggest an answer

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

Atlassian Community Events