Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,913
Community Members
 
Community Events
165
Community Groups

What is the best approach to populate a Jira Select List with dynamic data

Dear all,

We have a custom field named for instance List Of BD which is a single select list box. Usually we configure that list with static data directly from the configure section of the field.

Now in our new case, those data are coming from an external source that we have access to which render a list of DB in an array.

What we are looking for is the best way with script runner script to add those value to the list if it is not existing already.

What is the proper way to do this ? sample will help

regards

2 answers

1 accepted

0 votes
Answer accepted
Leo Community Leader Sep 21, 2021

Hi @serge calderara,

I used below code to update select list field options list dynamically(my use case was different and I was picking value from internal jira ticket field, so used listener in my case)

 

import com.atlassian.jira.component.ComponentAccessor

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

def value = //value to update in options list
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 ${updateValidationResult.errorCollection}")
}

 

BR,

Leo

0 votes

If the external database is of a supported type, then I'd reach for a database-picker field that is built into Scriptrunner - see https://docs.adaptavist.com/sr4js/latest/features/script-fields/built-in-script-fields/database-picker

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Improving the Create Issue Experience in Jira Service Management Cloud

Hello everyone!  We are very excited to announce some much needed changes to the issue create experience in JSM (the blue "create" button) at the top of the screen.  We have just starte...

225 views 10 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