Use Scriptrunner to add user to group

Sngy October 1, 2020

I'm searching for a solution that gives me an opportunity to add the user from Custom Field to specific JIRA Group by a post-function workflow or a listener.

I saw solutions like the following example in the community, but this does not seem to work anymore; the addUserToGroup method is not recognized.

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()

def user = "Neuling"

def group = groupManager.getGroup("Group1")

groupManager.addUserToGroup(user,group)

 

Thanks in advance!

 

 

 

 

 

2 answers

1 accepted

1 vote
Answer accepted
Niranjan
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.
October 2, 2020

Hi @Sngy ,

Kindly check this link for script to remove users from group

 

https://library.adaptavist.com/entity/remove-specified-users-from-a-group

 

Replace "removeUserFromGroup" with "addUserToGroup" and "userToRemove" with "userToAdd". It should work

Sngy October 2, 2020

Hi @Niranjan ,

it works now, thank you a lot!

0 votes
Vijay Sv May 5, 2021

This might help....

 

import com.atlassian.jira.component.ComponentAccessor


def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
final String groupName = "GROUPA" // the group you want to add users
def group = ComponentAccessor.groupManager.getGroup(groupName) // user names of the users to add
final List<String> userToAdd = ["USR1", "USR2", "USR3"]


userToAdd.each {
def usertoadd = userManager.getUserByName(it)
if (!usertoadd) {
log.warn("User: $userToAdd doesn't exist")
return

}

if (ComponentAccessor.getGroupManager().getGroupsForUser(usertoadd).contains(group)) {
log.warn("User: $usertoadd.username already in the group: $groupName")
return

}

if(!group) {
log.warn("Group: $groupName doesn't exist")

} else {

userUtil.addUserToGroup(group, usertoadd)
log.warn("User: $usertoadd.username added to the $groupName")

}

}

Ankuskum December 13, 2021

Hello Vijay,

I want to check if the "Jira-users" group is used in the notification scheme or not.

If yes then send an email.

Can you please help me in that

Suggest an answer

Log in or Sign up to answer