Groovy Code Setting a multi group picker custom field based on value of cascading select list value

We are trying to set a group picker (multiple groups) custom field (Stage Approver) using custom script post function.

Here is the script I am using. It is working for updating single group. Multiple groups is not working . Could anyone please guide me on this ?

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()

def multiGroupCf = customFieldManager.getCustomFieldObjectByName("Stage Approver") //multigroup picker custom field

def TeamDatagroup = groupManager.getGroup("TeamData Approvers") //jira group
def BOgroup = groupManager.getGroup("Business Owners") //jira group

issue.setCustomFieldValue(multiGroupCf, [TeamDatagroup])

 

How do i modify issue.setCustomFieldValue function such that i will be able to set more than one group?

 

Thanks 

Swathi

1 answer

1 accepted

2 votes

Hi Swathi,

Have you tried adding your groups to a list and then setting the field using that list?

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def multiGroupCf = customFieldManager.getCustomFieldObjectByName("Stage Approver") //multigroup picker custom field

def TeamDatagroup = groupManager.getGroup("TeamData Approvers") //jira group
def BOgroup = groupManager.getGroup("Business Owners") //jira group
def groupList = [TeamDatagroup, BOgroup]
issue.setCustomFieldValue(multiGroupCf, groupList)

Hi @Swathi Komirelli [Appfire]

Glad to hear it worked. Please accept my answer as the accepted answer so others with the same problem can find the solution easier.

Thanks for using ScriptRunner!

Josh

Hey @Joshua Yamdogo [Adaptavist], the above code works like a charm for post function but somehow it fails on custom listener. So I'm trying to update the group picker field when an issue is assigned to someone based on their group subscription. 

Thanks in advance.

 

Madhu

@Joshua Yamdogo [Adaptavist] How do i use this code to define the Group field based on the reporter's group? 

 

Here is my version of modified code: 

 

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def multiGroupCf = customFieldManager.getCustomFieldObjectByName("Assigned Group") //multigroup picker custom field

def Commercial = groupManager.getGroup("Commercial") //jira group
def EquityOps = groupManager.getGroup("EquityOps") //jira group
def Operations = groupManager.getGroup("Operations") //jira group
def groupList = [Commercial, EquityOps, Operations]
issue.setCustomFieldValue(multiGroupCf, groupList)

@Ankit Patel Could you explain more with a use-case example? Do you want to set the "Assigned Groups" field to all the groups that the reporter is in?

Josh

@Joshua Yamdogo [Adaptavist]

Within the project a user belongs to only one group. So I want to set the "Assigned Group" to the group user belongs to within the project.

Hi Ankit,

Something like this should work:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def assignedGroupCF = customFieldManager.getCustomFieldObjectByName("Assigned Group") //multigroup picker custom field

def assignedGroup = groupManager.getGroupsForUser(issue.getAssignee()) // get group for assignee
issue.setCustomFieldValue(assignedGroupCF , assignedGroup)

Thanks Joshua 

 

That did work out for me!

@Joshua Yamdogo [Adaptavist] - I have similar requirement, with one slight change in requirement.

We need to filter the group based on the project key. Let me know is it possible to achieve ?

 

https://community.atlassian.com/t5/Jira-questions/Restrict-the-group-picker-based-on-project-key/qaq-p/780903#M252492 

@Joshua Yamdogo [Adaptavist]

 

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def assignedGroupCF = customFieldManager.getCustomFieldObjectByName("Assigned Group") //multigroup picker custom field

def assignedGroup = groupManager.getGroupsForUser(issue.getReporter()) // get group for assignee
issue.setCustomFieldValue(assignedGroupCF,assignedGroup)

 

So I put this rule and i started to notice it changed the assigned group field to more than 1 groups the user belonged to. What I was looking for is if there is a way to select the group the user is in within the project only? 

 

Let me know if this is possible 

 

Thanks
Ankit

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

199 views 0 2
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you