Set Assignee on SubTask based on Custom user picker Field of Parent issue

Aaron Whigham June 18, 2018

I have spent all weekend trying different solutions proposed here for my issue.  I have not had success.  Please help.  The code that I get to work does not seem to be adding an actual user from the directory.  The entries appear to be string only.  I execute this code in the Additional Issue actions section of the create Sub-Task script during the create issue transition of the parent issue.

My code is below.

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User

import com.atlassian.jira.component.ComponentAccessor

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Direct Manager'}


java.lang.String user22 = sourceIssue.getCustomFieldValue(cf)
issue.setAssigneeId(user22)



1 answer

1 accepted

0 votes
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 18, 2018

Please try this code (ignore any compile time warning)

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Direct Manager'}


issue.setAssigneeId(sourceIssue.getCustomFieldValue(cf).getKey())

 

Aaron Whigham June 18, 2018

EXCELLENT!!!!!  IT WORKED IMMEDIATELY!!!

Aaron Whigham June 18, 2018

One more thing Tarun.... Now I have to add a specific user that I can hard code in for other subtask.  Any suggestions on this?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 18, 2018

Try this code

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.userManager
def userKey = userManager.getUserByName("<insert your username>").getKey()

issue.setAssigneeId(userKey)
Aaron Whigham June 18, 2018

Hello Tarun,

The script failed. Here is my code and the error message below.

Code:

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.userManager
def userKey = userManager.getUserByName("Paul Gomez").getKey()

issue.setAssigneeId(userKey)

 

Error Log

2018-06-18 09:35:55,612 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-06-18 09:35:55,613 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: NOP-195, actionId: 1, file: null
java.lang.NullPointerException: Cannot invoke method getKey() on null object
 at Script443.run(Script443.groovy:4)



Am I not entering the user name correctly?  Should I enter the full name with the email?

Aaron Whigham June 18, 2018

I got it!!!  I needed to enter the user name in this format:  first.last  

 

Great work Tarun!!

Suggest an answer

Log in or Sign up to answer