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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Super. Confirmed this works for my checkbox, thanks a bunch :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.