How to set a multi user picker as assignee

Swarna Radha September 4, 2018

Hi,

I want to set a multi user picker as assignee in a JIRA Software Project and I am using the code below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def brm_field = customFieldManager.getCustomFieldObjectByName("Approver (QA)")
ApplicationUser user = issue.getCustomFieldValue(brm_field) as ApplicationUser
UserManager userManager = ComponentAccessor.getUserManager();
issue.setAssignee((user))

 

An error message is displayed when i do the transition and the script is in the last position.Capture 1.PNG

 

Thanks

Swarna

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

Hello,

Try a code like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def brm_field = customFieldManager.getCustomFieldObjectByName("Approver (QA)")
def user = issue.getCustomFieldValue(brm_field) 
UserManager userManager = ComponentAccessor.getUserManager();
issue.setAssignee(user[0])

Cristian Rosas [Tecnofor]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

Exactly this

For your information @Swarna Radhayou can't set a multiuser picker value (collection) into a single user picker (not collection), so you need to take a single value. With the [0] you pick the first value of the collection.

Also, the code can be cleaned, it would be enough with this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def brm_field = customFieldManager.getCustomFieldObject("Approver (QA)")
def user = issue.getCustomFieldValue(brm_field)
issue.setAssignee(user[0])

 

Swarna Radha September 4, 2018

@Cristian Rosas [Tecnofor]

I am getting error on the last line 

issue.setAssignee(user[0])Capture 3.PNG
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

It is a static compilation error. It must work with this error as well. But if you want to get rid of this error, you can write like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def brm_field = customFieldManager.getCustomFieldObject("Approver (QA)")
List<ApplicationUser> user = issue.getCustomFieldValue(brm_field)
issue.setAssignee(user[0])
Swarna Radha September 4, 2018

@Alexey Matveev

 

Hi I am getting error and the script is failing. 

2018-09-05 09:22:10,469 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-09-05 09:22:10,470 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITCRD-3883, actionId: 91, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source)
 at Script1780.run(Script1780.groovy:13)

Capture 4.PNG

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

It means that the Approver (QA) custom field does not exist in your Jira.

Swarna Radha September 4, 2018

No, it exists. Please see screenshot.

Capture 5.PNG

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

That is right. It must be like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def brm_field = customFieldManager.getCustomFieldObjectByName("Approver (QA)")
List<ApplicationUser> user = issue.getCustomFieldValue(brm_field)
issue.setAssignee(user[0])
Swarna Radha September 4, 2018

@Alexey Matveev

IT is the same as above and I am getting on the line 

issue.getCustomFieldValue(brm_field)

"

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

Are you sure it is same? You can log the reference of the custom field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.getCustomFieldManager()


def brm_field = customFieldManager.getCustomFieldObjectByName("Approver (QA)")
log.error(brm_field)
List<ApplicationUser> user = issue.getCustomFieldValue(brm_field)
issue.setAssignee(user[0])

What value is logged? 

Swarna Radha September 4, 2018

@Alexey Matveev,

It is now working :) :). I have added it at the last position.

Thanks 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2018

Good to hear! :)

karthikeyan santhanam August 14, 2019

@Alexey Matveev I tried using the same you have provided still it is not setting the assignee from a multi-user picker custom field. I have this post function used in create transition as the last line. 
@Swarna Radha When you say, at the last position, do you mean to have the post function the "Custom script post-function (inline script)" at the last position or you are mentioning about in the groovy script? 

Any suggestion is appreciated. 

Suggest an answer

Log in or Sign up to answer