How to use Issue Properties in ScriptRunner Groovy scripts
Introduction
This guide explains how you can extract the stored properties from an issue using ScriptRunner. With that, you can use the information to perform other script runner functions
Entity properties are hidden and does not require a separate custom field to store the data. The values can be queried from REST API or JQL.
Example Scenario
Let's say you have five teams each with one leader and 5 agents. As an administrator, you want to create a Round Robin Assignment Rule to equally distribute the tickets to the agent in the team. One possible way is to use Lookup Manger and ScriptRunner
- The Lookup Manager use the value in the Reporter field, get the Round Robin Assignment Table to search the Leader column. If there is a matching row, store the value in the Team column as an entity property to the issue.
- Then write a ScriptRunner Post Function to retrieve the stored information and assign the ticket based on the Round Robin Rule.
How-to
1. Create a Lookup Table
The value in the Team column must be a valid JSON Object.
Eg. {"name": "Team A", "agents": {"agent_1":"Enrico", "agent_2" : "Maiya", "agent_3" : "Tyson", "agent_4" : "Leyla", "agent_5" : "Tate"}}
Check out how to create a lookup table
2. Create LookupManager: Add/Update Issue Property Function
Check out how to create workflow post function to update issue property
3. Create Script Post-Function(ScriptRunner)
- Select Custom script post-function
- Copy and paste the code below into the console
import groovy.json.JsonSlurper import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.bc.issue.properties.IssuePropertyService def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class) def parser = new JsonSlurper() // The entity key from workflow function def entityKey = 'team' // The entity name from the lookup table def teamEntityName = 'name' def agentsEntityName = 'agents' def issueProperties = issuePropertyService.getProperties(user, issue.getId()) def team = "" def agents = "" def assignee = "" issueProperties.each { property -> if (property.getKey().equals(entityKey)) { //Convert string to json, and obtain the value def json = parser.parseText( property.getValue()) team = json[teamEntityName] agents = json[agentsEntityName] assignee = roundRobinAssignment(agents); } } def roundRobinAssignment(agents) { //Write your own implementation here return agents["agent_1"] }
EntityPropertyService allows reading a specific property from the issue. If the lookup value is just a plain text string, you can use the following code example below to extract out the value
def lookupValue; //Load the team entity property from the issue if user has permission to read the issue def issueProperty = issuePropertyService.getProperty(user, issue.getId(), "team").getEntityProperty().getOrNull() if (issueProperty) { //extract the value out lookupValue = issueProperty.getValue(); } else { //set it to an empty string if there is no team entity property stored within the specified issue lookupValue = ""; }