Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,234
Community Members
 
Community Events
168
Community Groups

Add specific users to multi-user picker using Scriptrunner Listeners

Hello, I'm new to Scriptrunner, can someone help me solve it.

I wanted to set up Listener so that a performer is assigned for certain projects and in the UserPicker(multiple users) field add multiple users. These users may be inactive.

Part of the code for assigning the performer was found and works fine,
but there were problems with adding users to the UserPicker(multiple users) field.

How to do it correctly?
Thanks in advance for any help!

 

import java.lang.String
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import java.util.ArrayList

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def multiUserPicker = customFieldManager.getCustomFieldObject("customfield_10501") // -UserPicker(multiple users)

ArrayList<ApplicationUser> usersList = new ArrayList<ApplicationUser>()

//SCRUM - Jira Software project
//HRDesk - JSD project


//Users SCRUM
final String assigneeSCRUM = "UserScrum"
final List<String> usersSCRUM = ["User1", "User2", "User3"]

//Users HRDesk
final String assigneeDESK = "UserDesk"
final List<String> usersDESK = ["User3", "User4"]

if (issue.projectObject.key == 'SCRUM') {
def user = ComponentAccessor.userManager.getUserByName(assigneeSCRUM)
issue.setAssignee(user)

usersList.addAll(usersSCRUM)
issue.setCustomFieldValue(multiUserPicker, usersList)
}

if (issue.projectObject.key == 'HRDesk') {
def user = ComponentAccessor.userManager.getUserByName(assigneeDESK)
issue.setAssignee(user)

usersList.addAll(usersDESK)
issue.setCustomFieldValue(multiUserPicker, usersList)
}

1 answer

1 accepted

2 votes
Answer accepted

Hi @Julian Rodriguez

Reading through your description, I can confirm there is no direct approach to adding a new user to the user picker.

The user picker displays the existing users who are already included in Jira.

Hence, if you want to be able to access new users via the user picker, you must first add them to Jira itself.

You can follow this sample code from the Adaptavist Library, which provides the steps to create a new user.

Once the user is created, you can invoke the user in the user picker.

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram 

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Sorry, I may have misled you, Inactive users are disabled users in Active Directory.

My question is how to use Scriptrunner Listeners to add several
specific users ["User1", "User2", "User3"] to the UserPicker(multiple users).

It is not possible to add users ["User1", "User2", "User3"] to the ArrayList<ApplicationUser> and set the value ("customfield_10501")

I didn't find any examples of using setting values in ArrayList.

Hi @Julian Rodriguez

The approach you are taking will not work because when you are creating the List, you are setting the values as String, i.e. 

users ["User1", "User2", "User3"] to the ArrayList<ApplicationUser> 

When you should actually pass the value as an ApplicationUser, type something like:-

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


def userManager = ComponentAccessor.userManager
....
....
....


def
selectedUsers = [userManager.getUserByName('ram') , userManager.getUserByName('admin') , userManager.getUserByName('max')] as List<ApplicationUser>

I will assume that you are using the IssueCreated and IssueUpdated events. 

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

def issue = event.issue as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def userManager = ComponentAccessor.userManager

def multiUserPicker = customFieldManager.getCustomFieldObjectsByName('Multi User Picker').first()

def selectedUsers = [userManager.getUserByName('ram') , userManager.getUserByName('admin') , userManager.getUserByName('max')] as List<ApplicationUser>

issue.setCustomFieldValue(multiUserPicker, selectedUsers)

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Please note that the sample code provide is not 100% exact to your environment. Hence you will need to make the required modifications.

 

Below is a test screenshot:-

image1.png

When the issue is either Created or Updated, it will update the Multi-User Picker accordingly. In the image above, 3 users have been added to the Multi-User Picker.

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Your code works great in my environment.
I hope this solution will also help someone.
Thank you for your help!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events