How-to use Metadata values within Scripted Fields
This how-to requires the installation of ScriptRunner for Jira
Here we will show you how to use metadata values within calculated custom fields on an issue.
Calculated Text Field
Within Jira, Scriptrunner field of type Text Field (multi-line) was created and named Financial expenditure. (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 )
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:
HTML Text field
Within Jira, Scriptrunner field of type Html was created and named HTML Report. (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 )
For the example, we use different metadata values that are defined in the project, component and version (fix version).
Project
Component
Version
In the Custom Script Field HTML Report 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
StringBuilder sb = new StringBuilder();
sb.append("<p> Project - In Time : " + metadataService.getMetadataValue(issue.getProjectObject(), "In Time") + "</p>");
if (!issue.getComponents().isEmpty()) {
sb.append("<p> Component - Importance : " + metadataService.getMetadataValue(issue.getComponents().iterator().next(), "Importance") + "</p>");
}
if (!issue.getFixVersions().isEmpty()) {
sb.append("<p> Version - Hotfix : " + metadataService.getMetadataValue(issue.getFixVersions().iterator().next(), "Hotfix") + "</p>");
}
return sb.toString();
For an issue in which the Component, Fix Version was set, the field HTML Report looks like this.
Â