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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Mobily Monks likes this

Hi,

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

Regards

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..

@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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

579 views 5 20
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you