Need to set a couple custom User Fields (multiple) based on a drop down menu selection

Jeremy Cejka January 29, 2021

The purpose of this is try to make it easier on the life of our HR person.  The current process is to clone an existing ticket and modify.  Bad for business when everyone can see who is getting hired or fired.  

This is my first stab at writting groovy so bare with me, im a noob. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;
import com.atlassian.jira.user.util.UserManager;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def siteField = customFieldManager.getCustomFieldObject("customfield_14203")
def securityField = customFieldManager.getCustomFieldObject("customfield_14308")
def opsField = customFieldManager.getCustomFieldObject("customfield_14303")

def siteFieldValue = ((LazyLoadedOption)siteField).getValue()
def security = [] as List
def ops = [] as List

if (siteFieldValue == "ALX")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1a")
security << ComponentAccessor.getUserManager().getUserByKey("user2a")
ops << ComponentAccessor.getUserManager().getUserByKey("user3a")
log.debug "ALX"
}
if (siteFieldValue == "MLB")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1b")
security << ComponentAccessor.getUserManager().getUserByKey("user2b")
ops << ComponentAccessor.getUserManager().getUserByKey("user3b")
log.debug "MLB"
}
if (siteFieldValue == "SATX")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1c")
ops << ComponentAccessor.getUserManager().getUserByKey("user2c")
log.debug "SATX"
}
if (siteFieldValue == "Remote")
{
log.debug "Remote"
}


security = security.unique()
securityField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(securityField), security), new DefaultIssueChangeHolder())

ops = ops.unique()
opsField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(opsField), ops), new DefaultIssueChangeHolder())

 

This was taken from https://community.atlassian.com/t5/Jira-questions/How-to-write-postfunction-to-add-values-in-a-custom-field-based/qaq-p/748887

 

I edited to suite my needs.  I had to change the getCustomFieldObject, because the names contained spaces and the syntax checker didnt like it.  Maybe the wrong way, educate me.

The above script doesnt throw any syntax errors but it also doesnt fill in the custom fields and throws an error in the log

 

2021-01-29 19:02:21,654 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue HR-256 for user 'jcejka'. View here: https://devportal.researchinnovations.com/jira/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=HR+-+Out+Processing+Workflow&descriptorTab=postfunctions&workflowTransition=1&highlight=5
java.lang.NullPointerException
	at Script149.run(Script149.groovy:11)

 

Note that the security and ops are multi user fields.  

 

JIRA 8.5.4 Server on prem

Scriptrunner

 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.4
TAGS
AUG Leaders

Atlassian Community Events