Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Scriptrunner in Behaviours, need to dynamically update a single select custom field

Mark Johnson October 15, 2022

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).

 

illus1.png

 


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.

 

illus2.png

 


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.

 

illus3.png

 


When I comment out line 3 (Illustration 4), the field is populated with the options set in the custom field configuration (Illustration 5).

 

illus4.png

 

illus5.png

 

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

 

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
October 17, 2022

Hi @Mark Johnson

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:-

image1.png

 

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:-

test1.png

2. As expected, the list is not filtered, and all the options are available as shown below:-

test2.png

3. Next, the Behaviour is enabled as shown below:-

test3.png

4. As expected, the List options are also filtered accordingly

test4.png

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

Mark Johnson October 17, 2022

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

Suggest an answer

Log in or Sign up to answer