Will someone please provide a fix for populating my custom field (single select list) using scriptrunner code?
I'm using Jira Data Center v8.20. I have a custom single select list/drop-down list called “Project To Update”. In the configuration settings for the field, I configured the field with test options (Illustration 1).
Using behaviours (in the Initialiser section), I’m trying to dynamically set the options available in the select list when opening an issue in Jira (BEFORE the issue is actually created). Very simple, short code. For testing, on line 2, I’m just trying to limit the field options for the “Project To Update” field to 2 options (‘Project 1’ and ‘Project 2’). See Illustration 2.
This code (line 3, …setFieldOptions…) is obviously affecting the field, but not as I expected. The field (Project To Update) is empty. See Illustration 3. I'm guessing this may have to do with the way I'm creating the list (line 2), but I don't know how else to set it.
When I comment out line 3 (Illustration 4), the field is populated with the options set in the custom field configuration (Illustration 5).
As stated previously, line 3 is 'getting to' the field, but it's not updating the options list (it's clearing it). I'd really appreciate a resolution to this.
On a less important note, I’m also trying to set the field’s value to ‘Project 1’ (line 4 in the illustrations). Right now, the value is empty, even when the options are available and when only running line 4 (setFormValue). I’d appreciate help with this as well, though fixing the options list is my main goal.
Many thanks,
Mark
After going through your code, I can confirm that your approach will not work.
In your code, it appears that you are trying only to send a String value, i.e.
cField.setFormValue('Project 1')
This will not work. You must pass an Iterable type to it, i.e. a List or Map.
Below is the sample working code that I have tested with:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleList= getFieldByName('Sample List')
def availableOptions = ['Option 1', 'Option 2']
sampleList.setFieldOptions(availableOptions)
Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Behaviour configuration:-
Below are a couple of test screenshots for your reference:-
1. Firstly, a test is run when the Behaviour is disabled, as shown in the image below:-
2. As expected, the list is not filtered, and all the options are available as shown below:-
3. Next, the Behaviour is enabled as shown below:-
4. As expected, the List options are also filtered accordingly
Also, I have noticed that you are using an older version of ScriptRunner. It would be best to upgrade to the latest release, i.e. 7.2.0, as there have been many bug fixes and new features added
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
Hi Ram.
First, thank you for your reply. I really appreciate it.
Secondly, I used the exact 6 lines of code you used with 2 exceptions:
- changed 'Sample List' to 'Project To Update' (my field) and
- changed ['Option 1', 'Option 2'] to ['Project 1', 'Project 2'] (my field options).
When opening an issue, the list is still empty, like you see in my Illustration 3. Any ideas why? Maybe some internal Jira configuration settings/errors? Maybe my outdated Scriptrunner?
Thirdly (and less important at this point), can you provide the line of code that 'sets' the 'display' value of the list (not the entire list, but the option that's displayed when the list is collapsed)? I used the line below, which you state I need a list. Unless I'm mistaken with what '.setFormValue' does, I thought I needed a string here, or depending on the command syntax, a reference to the item number in the list for the option I want (i.e., list[0] = 'Project 1', list[1] = 'Project 2', etc.)
cField.setFormValue('Project 1')
Thanks,
Mark
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.