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

Script to add user to group based on field selection

Manuel Valcarcel
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 15, 2022

We are using a variation the script in https://library.adaptavist.com/entity/remove-specified-users-from-a-group to add a user to a specific group when an issue is created through our service desk portal.  Any idea how we could add the same user to groups selected in a group picker field?

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 15, 2022

Hi @Manuel Valcarcel 

Try the following. I tested it on my instance and works just fine!

import com.atlassian.jira.component.ComponentAccessor

// the group you want to remove users from
def groups = ["group1", "group2"]

// user names of the users to add
final List<String> usersToAdd = ["admin"]

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

ArrayList<com.atlassian.crowd.embedded.api.Group> finalGroups = new ArrayList<com.atlassian.crowd.embedded.api.Group>(groups.size())

for (group in groups){
finalGroups.add(ComponentAccessor.groupManager.getGroup(group))
assert group : "Could not find group with name $group"
}

usersToAdd.each {
def userToAdd = userManager.getUserByName(it)
userUtil.addUserToGroups(finalGroups, userToAdd)
}

 Let me know if that worked for you as well!

TAGS
AUG Leaders

Atlassian Community Events