Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,458,720
Community Members
 
Community Events
176
Community Groups

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

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

0 votes
Answer accepted

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

Thank you for the quick and thorough reply!

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.

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events