Groovy Script to add User to JIRA group

Serj Shcherbakov December 8, 2017

Hi There!


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

Thank You in advance,

4 answers

1 accepted

4 votes
Answer accepted
Alexey Matveev
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.
December 8, 2017

You need to read the value from the custom field and do something like this. <read user from custom field> depends on the type of your custom field

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()

def user = <read user from custom field>
def group = groupManager.getGroup("Group1") groupManager.addUserToGroup(user,group)
Serj Shcherbakov December 8, 2017

@Alexey Matveev wow, thank you for prompt answer!
On more question: could you provide a more detailed example of this row:

def user = <read user from custom field>
Alexey Matveev
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.
December 8, 2017

What is your custom field type for the user?

Serj Shcherbakov December 8, 2017

@Alexey Matveev is the User Picker (single user)

Alexey Matveev
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.
December 8, 2017
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)

def group = groupManager.getGroup("group1")

groupManager.addUserToGroup(user,group)
Like # people like this
Cem Yılmaz March 27, 2019

Hi,

I'm using script runner and I need to remove user from group. Can you write script like this please.

Regards

Nagaraju October 19, 2022

@Alexey Matveev 

I need the same script for getting users to group by using Multi-User picker field. help in this

Thanks

0 votes
QTR October 24, 2019

Hi,

 

Perhaps someone can help me with following, I'm trying to create a transition post-function that adds user to a group (from a user picker custom field). However, when running below script...

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def groupManager = ComponentAccessor.getGroupManager()
def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Field Name").getValue(issue)

def group = groupManager.getGroup("Group1")

groupManager.addUserToGroup(user,group)

...I get this error

019-10-24 22:36:14,574 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-10-24 22:36:14,575 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ISSUE-66, actionId: 11, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[user@user.com)]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.user.ApplicationUser' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.user.ApplicationUser(com.atlassian.jira.user.DelegatingApplicationUser)
 at Script74.run(Script74.groovy:6)
Vignesh Singh June 21, 2020

Hi all, 

I need the same script for getting users to group by using Multi-User picker field. Can anyone help in this, as its an urgent requirement.

@Alexey Matveev [cPrime]

 

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

def groupManager = ComponentAccessor.getGroupManager()
def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)

def group = groupManager.getGroup("group1")

groupManager.addUserToGroup(user,group)

above mentioned script is  working very well for adding single user in group. The requirement is to add multiple users to the group by having multi user picker custom field.

Please help me in this script..

Nagaraju October 19, 2022

Vignesh Singh

do you get the script for getting users to group by using Multi-User picker field ?

0 votes
Mobily Monks March 31, 2019

@Alexey Matveev 

 

Could you please suggest me whats I am doing here in  below groovy script.

I am attempting to add users listed in below user list to a respective group Group1. 

 

I am using Script Console to perform this task.

 

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()

def user = ["Chandrakanta Swain", "Ikramirshad Khan", "Mohammed Shaheen", "Nagasudheerkumar Kappala"];
def group = groupManager.getGroup("Group1")

userManager.getAllUsers().each {
user -&gt; groupManager.addUserToGroup(user,group)
}

 

 

2019-03-31 13:49:18,240 WARN [common.UserScriptEndpoint]: Script console script failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script57.groovy: 10: expecting '}', found '-' @ line 10, column 6. user -&gt; groupManager.addUserToGroup(user,group) ^ 1 error

Alibek Malikov July 9, 2019

user -&gt; groupManager.addUserToGroup(user,group

Here you have an error on which swears in the logs

Like Alibek likes this
Alibek Malikov July 9, 2019

And in the groove language this syntax is not used.

0 votes
Serj Shcherbakov December 8, 2017

@Alexey Matveev perfect! Many-many thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events