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)
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:
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])
}
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])
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Alejandro Suárez García,
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.