Scriptrunner Post Function - Sets Component based on Single List Custom Field

Matt Bausher February 12, 2019

Hello,

I am trying to have a post-function that sets the component based off of a required Single List custom field (Ex: selecting the DEB option sets the component to Machien Shop - DEB).  However, when executed the custom scriptrunner script fails stating the value is null.

I've tried referencing other similar topics but wasn't able to find the obvious answer.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

//a map with single list options to associated component
def componentsMap = [
"DEB" : "Machine Shop - DEB",
"GR" : "Machine Shop - GR",
"LC" : "Machine Shop - LC",
"LM" : "Machine Shop - LM",
"MC" : "Machine Shop - MC",
"MCP" : "Machine Shop - MCP",
"MCX" : "Machine Shop - MCX",
"MM" : "Machine Shop - MM",
"MXP" : "Machine Shop - MXP",
]

def issue
issue = issue as MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def selectList = customFieldManager.getCustomFieldObject("customfield_14161")

//Catches if the field does not exist
if (! selectList) {
return
}

def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption

//Catches if there is no value for the select list
if (! selectListValue) {
return
}

def componentManager = ComponentAccessor.projectComponentManager
def componentName = componentsMap.get(selectListValue.value)
def component = componentManager.findByComponentName(issue.projectObject.id, componentName)

if (component) {
componentManager.updateIssueProjectComponents(issue, [component])
}

 The script doesn't show any errors until I run a test issue and it produces the following.

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-02-12 07:47:53,807 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-02-12 07:47:53,807 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PRD-14901, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
	at Script78.run(Script78.groovy:28)

2 answers

1 accepted

0 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

Hi again @Matt Bausher,

I tested this scripted postfunction (changed back to your customfield ID) and it works fine for me. The only two requirements are:

  • To have a Custom Field with that options - DEB, GR, LC... etc
  • To have the Components created in the project you want.

Here is the code (put it in the first postfunction place)

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ProjectComponentManager componentManager = ComponentAccessor.projectComponentManager

//a map with single list options to associated component
def componentsMap = [
"DEB": "Machine Shop - DEB",
"GR" : "Machine Shop - GR",
"LC" : "Machine Shop - LC",
"LM" : "Machine Shop - LM",
"MC" : "Machine Shop - MC",
"MCP": "Machine Shop - MCP",
"MCX": "Machine Shop - MCX",
"MM" : "Machine Shop - MM",
"MXP": "Machine Shop - MXP",
]

CustomField selectList = customFieldManager.getCustomFieldObject(14161L)
Option selectListValue = issue.getCustomFieldValue(selectList) as Option

if (selectListValue) {
String componentName = componentsMap.get(selectListValue.getValue())
ProjectComponent component = componentManager.findByComponentName(issue.projectObject.getId(), componentName)

if (component) issue.setComponent([component])
}
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

You can also ignore that map, and just use the following code:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ProjectComponentManager componentManager = ComponentAccessor.projectComponentManager

CustomField selectList = customFieldManager.getCustomFieldObject(14161L)
Option selectListValue = issue.getCustomFieldValue(selectList) as Option

if (selectListValue) {
String componentName = "Machine Shop - " + selectListValue.getValue()
ProjectComponent component = componentManager.findByComponentName(issue.projectObject.getId(), componentName)

if (component) issue.setComponent([component])
}
Like Matt Bausher likes this
Matt Bausher February 12, 2019

Hello @Alejandro Suárez - TecnoFor,

Both of the options you listed above worked out perfectly for me and I personally prefer the option without the map.

Thank you for taking a moment to help me out!

Matt Bausher

0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

Hi @Matt Bausher the error says that the CustomField don't have any value when you execute the script. Let me do a few tests and I give you an answer.

Suggest an answer

Log in or Sign up to answer