How to add dynamic options to a single select list field

SWAPNIL SRIVASTAV November 3, 2019

I have a requirement of a select list custom field for a particular project which can have dynamic options such as:

There is a number field : Bnum

System field in jira: Summary

The option should appear like "<Value of Bnum><space><Value of summary>"

So the no of options dynamically increase with the number of issues that are created within that particular project.

e.g. Issue 1: Bnum=123

                   Summary= abcd

      Issue 2: Bnum=456

                 Summary = efgh

Then field options should be:

  • 123 abcd
  • 456 efgh

Kindly suggest a plugin or script to do so. Any help would be appreciated.

2 answers

1 accepted

1 vote
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2019

Hi @SWAPNIL SRIVASTAV ,

Basically it's bad idea of giving more options/space to all to add new values to select list field. you'll end up with many unwanted values. you can consider labels 

 

But if you still want to go ahead

You can go with ScriptRunner's Custom listener. on your case listener for Issue created event

I'm attaching sample snippet for your reference you may need to modify some lines here

1. Getting custom field and append with summary value

2. updating user should have valid permission. if not you can use particular user id in the script who is having the permission to modify field values

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def issueService = ComponentAccessor.getIssueService()

def issue = event.issue
def value = issue.summary as String
def updField = customFieldManager.getCustomFieldObjectByName("Field name for which you want to add new values/options")

def fieldConfig = updField.getRelevantConfig(issue)
def currentOptions = optionsManager.getOptions(fieldConfig)
def newSeqId = currentOptions*.sequence.max() - 1
def option = optionsManager.createOption(fieldConfig, null, newSeqId, value)

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
addCustomFieldValue(updField, option.optionId.toString())
addCustomFieldValue(value, null)
}
def updateValidationResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (updateValidationResult.isValid()) {
issueService.update(currentUser, updateValidationResult)
}
else {
log.warn("Failed to update issue: ${issue.key}: ${updateValidationResult.errorCollection}")
 

 

BR,

Leo

SWAPNIL SRIVASTAV November 4, 2019

Hello @Leo ,

Thank you for the response. 

I am going to try the script you have suggested. However, I need to display the options on Create screen and let the user choose the desirable option. 

The scenario is, I need the summary and value of Bnum (Number field) of previous created issues in the project and then concat them to form a string and display that string as options. So when there are 500 existing issues in the project, when the 501(th) issue is being created, the field should be displayed on the Create screen with 500 options of <Bnum><space><Summary>, each with the previous issues' Bnum and Summary.

I understand that it is not wise to have so many options in a single field and I am trying for some other approach as well, in the meantime, I need this functionality to work.

Thanks and Regards,

Swapnil Srivastav

SWAPNIL SRIVASTAV November 4, 2019

Hello @Leo , PFB link:

https://community.atlassian.com/t5/Jira-questions/How-to-add-dynamic-options-into-a-Select-List-custom-field-using/qaq-p/1217509

(Here, I have mentioned the Behaviour Script that I have used).

Kindly have a look and let me know, where am I going wrong.

Thanks and Regards,

Swapnil Srivastav.

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2019

Hi @SWAPNIL SRIVASTAV ,

I'm not sure what is the issue/error you are getting.

but when I look the flow of your code seems to be not good practice.

Why because for each and every time your number field changes you are running JQL query for all existing issue and adding them as values to your select list field. 

E.g. for the first time it'll add 10 values for 10 existing issues and if I update number field on anyone of those issues your script will search for all 10 issues and add them again.

 

I never tried code for updating existing option(s) of select list field.

for your requirement the above script on custom listener(not on behavior) on issue create issue event will work. I'm just adding number field value appending snippet as well

def numField = customFieldManager.getCustomFieldObjectByName("Number Field name")
nVal = issue.getCustomFieldValue(numField) as String
def value = nVal+" "+issue.summary as String

 

NOTE: if you add this script for issue create event listener. while creating new issue it'll populate values till last last issue created. when create button hits this script will run and will fetch number and summary value of currently created ticket and then add it as a new option for your select list

One more thing you should note here I used currentLoggedin user(which is me, having permission to add values to select list)

 

For adding previous tickets values to select list you can run the same script in script console(instead of fetching current issue details run your jqlparser and add them)

 

 

BR,

Leo

SWAPNIL SRIVASTAV November 5, 2019

Hello @Leo ,

Alright, I get your point. I agree with you.

Please let me know whether using Select List Conversions or Database picker of Script Runner will be a better approach.

https://scriptrunner.adaptavist.com/4.3.5/jira/behaviours-conversions.html

and

https://scriptrunner.adaptavist.com/latest/jira/script-fields/database-picker.html

 

Thanks and Regards,

Swapnil Srivastav

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2019

Which field you are going to convert to select list? summary field?

iWhat I understood was you have number field and and select list field

Fetch number field and value and summary and add that as new option to your select list field 

it's purely based on your requirement, but as per my knowledge for your above requirement there is no conversion or script field is required

1. Create Number Field

2. Create Select List field

3. Write your script in custom listener

 

BR,

Leo

SWAPNIL SRIVASTAV November 5, 2019

@Leo ,

Ok, I get it.

Thank you very much for your response

Best Regards,

Swapnil Srivastav

0 votes
Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2019

Hi,

I will suggest nfeed plugin or scriptrunner. 

I think nfeed fit more you requirement, yiu can easily query your database to find those values to display.

SWAPNIL SRIVASTAV November 3, 2019

Hello @Mohamed Benziane

Thank you for the immediate response.

I checked nFeed or Elements Connect, its a paid plugin. I would not be able to use that.

Could you please suggest, how can it be achieved using Script Runner ?

Thanks and Regards,

Swapnil Srivastav.

Mohamed Benziane
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2019

Sorry but im not good at groovy code, see below the answer of @Leo 

Suggest an answer

Log in or Sign up to answer