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?
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.