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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,464,912
Community Members
 
Community Events
176
Community Groups

Groovy Script to add User to JIRA group

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

3 votes
Answer accepted

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)

@Alexey Matveev _Appfire_ 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>

What is your custom field type for the user?

@Alexey Matveev _Appfire_ is the User Picker (single user)

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

Hi,

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

Regards

Nagaraju I'm New Here Oct 19, 2022

@Alexey Matveev _Appfire_ 

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

Thanks

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)

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 I'm New Here Oct 19, 2022

Vignesh Singh

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

@Alexey Matveev _Appfire_ 

 

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

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

Here you have an error on which swears in the logs

Like Alibek likes this

And in the groove language this syntax is not used.

@Alexey Matveev _Appfire_ perfect! Many-many thanks!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events