How to set a listener condition with field Id instead of field name

Michiel Schuijer October 21, 2019

Hi, I have created a Scriptrunner listener which should be triggered to create a sub-task upon creation of a new issue with the condition that a certain field has a specific value.

Initially I tried this with a checkbox field but it did not seem to meet the value and I was not able to find out how to check the validity of the condition.

I then went on to pick a different field but ran into the issue that there are several fields with the same name and so I would need to specify the field ID instead of the name but I was unable to find out how to specify this as a condition.

I got it to work with the "Has select list value equal to" and scripted condition for select list (single choice) "cfValues['Captured In Release']?.value == 'Yes'" but now I want to achieve the same with the field ID and optionally would like to know if this will work with a checkbox value as well.

Can anyone assist me with that?

Thanks a lot!

Regards

Michiel

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 21, 2019

Checkbox fields are represented as an array of options. Just like a multi-select field.

Each option represents a separate checkbox.

So you can look for if a box is checked with:

cfValues['Check box field name']*.value.contains('checkBox Value')

If you want to access fields by their ID, then you can't use the cfValues (this is a shortcut provided by scriptrunner only in certain contexts, so might as well learn the full way to do it).

The get the value of a checkbox from the custom field id (123456 used in the example, you will need all these lines of code:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def checkBoxId = 123456
def checkBoxFieldObject = customFieldManager.getCustomFieldObject(checkBoxId )
def listOfCheckedOptions = issue.getCustomFieldValue(customFieldObject)
def checkBoxIsChecked = 'my checbox value' in listOfCheckedOptions*.value
Michiel Schuijer October 22, 2019

Thank you for the quick and thorough reply!

Michiel Schuijer October 22, 2019

I have tested it and get a couple of errors with your code sample:

[Static Type checking] - The variable [customFieldObject] is undeclared. @ line 5, column 54.

This can be solved, I think, by changing line 4 {{def checkBoxObject}} to {{def customFieldObject}}, as doing this will clear that first error.

But then there is the second error:

[Static Type checking] - No such property: value for class: java.lang.Object @ line 6, column 34.

Don't know yet how to overcome the second one and if the first change I made is correct.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2019

Yeah, sorry, I wrote it one way then changed some variable name to match your case and forgot one.

This is what I meant to post (changed line 6 instead of 4):

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def checkBoxId = 123456
def checkBoxFieldObject = customFieldManager.getCustomFieldObject(checkBoxId )

def listOfCheckedOptions = issue.getCustomFieldValue(checkBoxFieldObject)
def checkBoxIsChecked = 'my checbox value' in listOfCheckedOptions*.value

But the second error is normal. The scriptrunner code editor does static type checking. But Groovy allows non-static type assignment. In this case, the editors is not sure what will be returned in listOfCheckdOptions, so it can't validate that "value" is a valid attribute. 

But at runtime, groovy will know what to do with that and it will work.

I personally prefer to keep the code as simple as possible... rather than as complete and explicit as possible. So I would use the above. But if you come from a pure java world and those errors bug you, then you would need to import classes for all the different types of objects and handle some type transformations. For example (using console, so I defined my own issue object and used some field id and values that exist in my environment):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def customFieldManager = ComponentAccessor.customFieldManager
def checkBoxId = 10300
def checkBoxFieldObject = customFieldManager.getCustomFieldObject(checkBoxId )
def issue = ComponentAccessor.issueManager.getIssueObject('JSP-1922')
def listOfCheckedOptions = issue.getCustomFieldValue(checkBoxFieldObject) as List<Option>
def checkBoxIsChecked = 'Security' in listOfCheckedOptions*.value
Michiel Schuijer October 23, 2019

Super. Confirmed this works for my checkbox, thanks a bunch :)

TAGS
AUG Leaders

Atlassian Community Events