How to support Component field in Lookup Manager

Scenario

For the list of supported fields, refer to

The Component field is not supported in the Source Field and Destination Field. We can solve this problem by using both the ScriptRunner and Lookup Manager together.

How-to

Step 1: Create a Lookup Table

Component

Custom_Select

Category

Component

Custom_Select

Category

Internal

LOOKUP

INTERNAL

Business

CQL

BUSINESS

Client

OOO

CLIENT

 

Step 2:  Create Custom Script Post-Function(ScriptRunner)

Step 3: Copy and paste the code below into the console 

Case 1: Component Field as Source Field

To populate a custom field based on looking up Component in a table managed by Lookup Manager

  • The Lookup Manager gets the Component Table to search the Component column for the value Internal. If there is a matching row, return the value in the Custom_Select column.

  • Then set the value to custom field Custom_Select on the created issue. 

import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.akelesconsulting.jira.plugins.rest.LookupService import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.project.Project import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.IssueManager import org.apache.log4j.Logger import org.apache.log4j.Level import groovy.json.JsonSlurper @WithPlugin("com.akelesconsulting.jira.plugins.LookupManager") @PluginModule LookupService lookupService def log = Logger.getLogger("LookupManager") log.setLevel(Level.DEBUG) def issueManager = ComponentAccessor.getIssueManager() def customFieldManager = ComponentAccessor.getCustomFieldManager() def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() log.debug("***** Populate a custom field based on looking up Component in a table managed by Lookup Manager *****") def components = issue.getComponentObjects(); log.debug ( issue.getComponentObjects()*.name ) //print out the name of each of the components if (components) { def tableName = "Component Table" def sourceCol = "Component" def sourceVal = components[0].name //E.g. "Internal" def destCol = "Field_1" int tableId = lookupService.getTableIdByName(tableName) //tableId = 1 def matchingValuesArray = lookupService.lookup(tableId, sourceCol, sourceVal, destCol) // matchingValuesArray = [LOOKUP] def value = matchingValuesArray[0] log.debug("Matching Vallue: " + value) def customFieldName = "Custom_Select" //Custom Select Field def cfSelect = customFieldManager.getCustomFieldObjectByName(customFieldName) assert cfSelect: "Could not find custom field with name $customFieldName" def availableOptions = ComponentAccessor.optionsManager.getOptions(cfSelect.getRelevantConfig(issue)) def optionToSet = availableOptions.find {it.value == value} assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}" issue.setCustomFieldValue(cfSelect, optionToSet) issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false) }

Case 2: Component Field as Destination Field

To set the Component value based on looking up a custom field in a table managed by Lookup Manager

  • The Lookup Manager gets the Component Table to search the Category column for the value INTERNAL. If there is a matching row, return the value in the Component column.

  • Then set the value to Component field on the created issue. 

import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.akelesconsulting.jira.plugins.rest.LookupService import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.project.Project import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.component.ComponentAccessor import org.apache.log4j.Logger import org.apache.log4j.Level import groovy.json.JsonSlurper import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.util.collect.MapBuilder; import java.util.List; import org.ofbiz.core.entity.GenericValue; import org.ofbiz.core.entity.GenericEntityException; @WithPlugin("com.akelesconsulting.jira.plugins.LookupManager") @PluginModule LookupService lookupService def log = Logger.getLogger("LookupManager") log.setLevel(Level.DEBUG) def issueManager = ComponentAccessor.getIssueManager() def customFieldManager = ComponentAccessor.getCustomFieldManager() def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() log.debug("***** Set the Component value based on looking up another custom field in a table managed by Lookup Manager *****") def customFieldName = "Category" //Custom Select Field def cfSelect = customFieldManager.getCustomFieldObjectByName(customFieldName) assert cfSelect: "Could not find custom field with name $customFieldName" if (issue.getCustomFieldValue(cfSelect)) { def tableName = "Component Table" def sourceCol = "Category" def sourceVal = issue.getCustomFieldValue(cfSelect).getValue() // INTERNAL def destCol = "Component" def tableId = lookupService.getTableIdByName(tableName) //tableId = 1 def matchingValuesArray = lookupService.lookup(tableId, sourceCol, sourceVal, destCol) // matchingValuesArray = [Internal] def value = matchingValuesArray[0] log.debug("Matching Vallue: " + value) def project = issue.getProjectObject() def projectComponentManager = ComponentAccessor.getProjectComponentManager() def component = projectComponentManager.findByComponentName(project.getId(), value) assert component: "Could not find component name $value" issue.setComponent([component]) //Update Assignee field def componentType = component.getAssigneeType() def assignee = null switch (componentType) { //0 = Project Default, 3 = Component Lead, 2 = Project Lead, 3 = Unassigned, case 0: def projectDefault = issue.getProjectObject().getAssigneeType() //2 = Project Lead, 3 = Unassigned if (projectDefault == 2) { assignee = issue.getProjectObject().getProjectLead() } break case 1: assignee = component.getComponentLead() break case 2: assignee = issue.getProjectObject().getProjectLead() break case 3: assignee = null } log.debug("Assignee: " + assignee) issue.setAssignee(assignee) issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false) }

Step 3: Put the post function after the “Create the issue originally”

The custom script runner post-function should be placed after the Create the issue originally transition.