Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

109 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you