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 as shown here.
In the "Create" transition of a workflow a "Custom script post-function" (further details about Scripted Postfunctions can be found here: https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-post-functions-tutorial.html ) has to be added and the following script has to be provided:
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.
How-to Use Metadata values within Scripted Fields - in Progress
This how-to requires the installation of ScriptRunner for Jira
Here we will show you how to use metadata values within a "calculated" custom field on an issue.
Within Jira, Scriptrunner field of type "Text Field (multi-line)" was created (further details can be found here https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html and here https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/table-custom-field.html ) and named "Financial expenditure".
For the example we use the metadata "Hourly rate" which is set in the project (further details about Project Screens can be found here: How to customize the Metadata Project Screen as Jira-Administrator). As shown here, it has the value 105.5.
In the Custom Script Field "Financial expenditure" the following Groovy script must be added under "Script".
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.customfields.manager.OptionsManager import java.text.NumberFormat; import java.util.Locale; @WithPlugin("com.osoboo.jira-metadata-plugin") @PluginModule MetadataService metadataService Long timeSpent = issue.timeSpent NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US); if (timeSpent > 0) { Double hourlyRate = Double.parseDouble(metadataService.getMetadataValue(issue.getProjectObject(), "Hourly rate")) return currencyFormat.format(timeSpent / 3600 * hourlyRate); } else { return currencyFormat.format(0.00);; }
For an issue on which 3 hours were logged, the following is now displayed..