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,

3 answers

1 accepted

3 votes
Answer accepted
Alexey Matveev [cPrime] Community Leader Dec 08, 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)

@Alexey Matveev [cPrime] 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 [cPrime] Community Leader Dec 08, 2017

What is your custom field type for the user?

@Alexey Matveev [cPrime] is the User Picker (single user)

Alexey Matveev [cPrime] Community Leader Dec 08, 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 Mobily Monks likes this
Cem Yılmaz Mar 27, 2019 • edited

Hi,

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

Regards

0 votes

@Alexey Matveev [cPrime] perfect! Many-many thanks!

0 votes
Mobily Monks Mar 31, 2019

@Alexey Matveev [cPrime] 

 

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Marketplace Apps & Integrations

Demo Den Ep. 4: Continuous Integration with CircleCI & Jira Software

Get ready! Demo Den Episode 4 is coming your way on Tuesday, May 28, 2018 with a Continuous Integration and Delivery special demo. CircleCI Director of Solutions Engineering, Eddie Webb will show us ...

709 views 0 4
Join discussion

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