How-to use Metadata values within Custom script post-functions

This how-to requires the installation of ScriptRunner for Jira

Here we will show you how to transfer metadata values to a Select List during the creation of an issue.

For the example we use the metadata Service Level which can be set for a user (further details about the User Screen can be found here: How-to customize the Metadata Customer Screen as Jira-Administrator). As shown here, this metadata can have the value Platinum.

In the Jira instance the field Reporter - Service Level was added and configured.

In the Create transition of a workflow a Custom script post-function has to be added and the following script has to be provided:
(Further details about Scripted Postfunctions can be found here: https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-post-functions-tutorial.html )

import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.osoboo.jira.metadata.MetadataService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.customfields.manager.OptionsManager @WithPlugin("com.osoboo.jira-metadata-plugin") @PluginModule MetadataService metadataService OptionsManager optionsManager = ComponentAccessor.getOptionsManager() CustomFieldManager customFieldManager= ComponentAccessor.getCustomFieldManager() def cf = customFieldManager.getCustomFieldObjectByName("Reporter - Service Level") // Reporter - Service Level is the of my Select custom field def fieldConfig = cf.getRelevantConfig(issue) def option = optionsManager.getOptions(fieldConfig).getOptionForValue(metadataService.getMetadataValue(issue.getReporter(), "Service Level"), null) issue.setCustomFieldValue(cf, option)

The script copies Service Level metadata from the Reporter to the Reporter - Service Level field. The result looks like this: