Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Goal

This section shows the instructions to use the ScriptRunner together with Lookup Manager to append values to multi-select fields.

Pre-requisites

✔️ Create a lookup table
✔️ Lookup Table Edit permission
✔️ ScriptRunner installed

Demonstration

Instead of overwriting the existing field value, I would like to append the value to a multi-select custom field.

...

Code Block
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.akelesconsulting.jira.plugins.rest.LookupService

@WithPlugin("com.akelesconsulting.jira.plugins.LookupManager")
 
@PluginModule
LookupService lookupService

int tableId = lookupService.getTableIdByName("App Management Table")
def matchingValuesArray = lookupService.lookup(tableId, "Product Owner", "Adam", "Project Key")

def customFieldManager = ComponentAccessor.customFieldManager
// Name of the multi-select custom field to change
def customFieldName = "Product List"
def cf = customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValue = issue.getCustomFieldValue(cf) as ArrayList
if (cfValue == null) {
    cfValue = new ArrayList()
}

def optionsManager = ComponentAccessor.getOptionsManager()
def newValues = cfValue
matchingValuesArray.each { val ->
    def option = optionsManager.getOptions(cf.getRelevantConfig(issue)).find {it.value == val.trim()}
    newValues += [option]
}
issue.setCustomFieldValue(cf, newValues)

...

Learn More