Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Add specific users to multi-user picker using Scriptrunner Listeners

Julian Rodriguez August 15, 2022

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

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2022

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 

Julian Rodriguez August 17, 2022

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2022

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

Julian Rodriguez August 19, 2022

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!

Volodymyr
Contributor
February 9, 2024

The approach that was described by @Ram Kumar Aravindakshan _Adaptavist_ works, but what if I don't want to update the field completely every time, but only add a new user.
For example, I have a task to add users to a similar field when they log time in a ticket for the first time. What if there is a ticket where the field contains an inactive user and I need to add a new one? I don't want to delete the old(inactive) one, just add a new user.
If I take all users and add them to the field, the inactive ones will not be added, therefore deleted.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2024

Hi @Volodymyr

The example given here doesn't provide any approach to delete users.

For your requirement, you will basically need to update the existing list of users by incrementing a new user to it.

For example:-

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

....
....
//
//Your code
//
....
....

selectedUsers << userManager.getUserByName('anotheruser')

This will just increment the new user to the User Picker list without removing any of the previous users.

Thank you and Kind regards,
Ram

Volodymyr
Contributor
February 9, 2024

I don't fully understand how it should work using your option.
Every time a user logs time, it should be added to the field. I need to leave inactive users who are already in the field (e.x., they will finish their work and quit in 1-2 months but we need them in statistics). Next I will add a check to add a user who is not already in the field.
For example, for now I have this test code.

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

def worklog = event?.getWorklog()
def issue = ComponentAccessor.getIssueManager().getIssueObject(worklog?.getIssue()?.getId())
def userCreatedWorklog = worklog?.getAuthorObject()

log.warn("worklog: ${worklog}")
log.warn("issue: ${issue}")
log.warn("userCreatedWorklog: ${userCreatedWorklog}")

def cfContributors = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(25902)
def cfContributorsValues = issue.getCustomFieldValue(cfContributors)

Collection<ApplicationUser> cfContributorsValue = []
log.warn("cfContributorsValue BEFORE: ${cfContributorsValue}")

if (cfContributorsValues == null) {
cfContributorsValue.add(userCreatedWorklog)
log.warn("cfContributorsValue AFTER SINGLE: ${cfContributorsValue}")

issue.setCustomFieldValue(cfContributors, cfContributorsValue)
ComponentAccessor.getIssueManager().updateIssue(userCreatedWorklog, issue, EventDispatchOption.ISSUE_UPDATED, false)
} else {
cfContributorsValue.addAll(issue.getCustomFieldValue(cfContributors))

log.warn("cfContributorsValue AFTER - values from the issue: ${cfContributorsValue}")
cfContributorsValue.add(userCreatedWorklog)

log.warn("cfContributorsValue AFTER - values from the issue + new one: ${cfContributorsValue}")
log.warn("issue.getCustomFieldValue(cfContributors).add(userCreatedWorklog): ${issue.getCustomFieldValue(cfContributors).add(userCreatedWorklog)}")

issue.setCustomFieldValue(cfContributors, issue.getCustomFieldValue(cfContributors).add(userCreatedWorklog))
ComponentAccessor.getIssueManager().updateIssue(userCreatedWorklog, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2024

Hi @Volodymyr

From your description, your goal is to keep incrementing the User Picker field whenever a user makes a change to it.

The User Picker field value is based on the value of the selectUsers List, i.e.

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

issue.setCustomFieldValue(multiUserPicker, selectedUsers)

Hence, whenever a change is made to your issue by a user, you will need to update the selectedUsers list by incrementing, i.e. adding another user to it, i.e.:-

selectedUsers << userManager.getUserByName('anotheruser')

issue.setCustomFieldValue(multiUserPicker, selectedUsers)

or if you want to do it by the currently logged-in user, you can use

selectedUsers << Users.loggedInUser()

issue.setCustomFieldValue(multiUserPicker, selectedUsers)

Thank you and Kind regards,
Ram

Volodymyr
Contributor
February 13, 2024

@Ram Kumar Aravindakshan _Adaptavist_ 
Thank you for the detailed explanation.
From your description, I can understand that the list is created from active users, but what if there is an inactive user in the field and I need to add a new user to the field who is active? As a result, there will be inactive and active users, which will be displayed in reports. Does your option cover this description?

Volodymyr
Contributor
February 16, 2024

I've tested the code for inactive users and it's working. You may have deleted or inactive users in your field, but adding new users retains all values.

/*
When the one event, WorklogCreatedEvent, occurs, the current listener runs.
When a worklog is created in an issue, its creator is added to the field "Contributors" (User Picker (multiple users)).
If there is an already deleted or inactive user in the field, such values are not deleted while updating.
*/

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

// Worklog, Issue, Worklog Creator
def worklog = event?.getWorklog()
def issue = ComponentAccessor.getIssueManager().getIssueObject(worklog?.getIssue()?.getId())
def userCreatedWorklog = worklog?.getAuthorObject()

// Customfield Contributors and its values for the issue
def cfContributors = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(25902)
def cfContributorsValues = issue.getCustomFieldValue(cfContributors) as List<ApplicationUser>

// List to add the first user to the field
def firstContributor = [] as List<ApplicationUser>

// If the field is empty, we need to add the first user
// If the field already contains a user who logs time, do nothing
// If the field doesn't contain a user who logs time, add it to the existing values
if (cfContributorsValues.equals(null)) {
    firstContributor.add(userCreatedWorklog)

    issue.setCustomFieldValue(cfContributors, firstContributor)
    ComponentAccessor.getIssueManager().updateIssue(userCreatedWorklog, issue, EventDispatchOption.ISSUE_UPDATED, false)
} else if (cfContributorsValues.contains(userCreatedWorklog)) {
    return
} else {
    cfContributorsValues << userCreatedWorklog

    issue.setCustomFieldValue(cfContributors, cfContributorsValues)
    ComponentAccessor.getIssueManager().updateIssue(userCreatedWorklog, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

TAGS
AUG Leaders

Atlassian Community Events