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,556,816
Community Members
 
Community Events
184
Community Groups

Set Approver Based on the Requestor's Group

Alvin
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.
Sep 21, 2018

Hi Community. I'm new to groovy and I just want to seek help here. How can I set approver/s based on the requestor's group upon creation using post function scriptrunner

For example , Requestor 1 belongs to the Group 1 and Requestor 2 belongs to the Group 2 , if Requestor 1 creates a ticket, Approver must be the Approver 1 , and same rule applies to Requestor 2

any comments/suggestion is really appreciated. If you have sample code to work on, that would be great. Thank you!

 

2 answers

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
Sep 25, 2018

Hi Alvin, 

Assuming your approvers field is a multi user-picker type custom field and requestor is the user creating the issue, the code for a post function is as follows. Make sure to put in on your 'Create' transition.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager


ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
GroupManager groupManager = ComponentAccessor.getGroupManager()
UserManager userManager = ComponentAccessor.getUserManager()
CustomField approversField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("approversFieldName")
List<ApplicationUser> approversList = new ArrayList<>()

Map<String, String> approverMap = new HashMap<>()
approverMap.put("group1", "approver1")
approverMap.put("group2", "approver2")

Collection<String> groups = approverMap.keySet()
for(int i = 0; i < groups.size(); i++){
if(groupManager.isUserInGroup(currentUser, groups[i])){
approversList.add(userManager.getUserByName(approverMap.get(groups[i])))
break
}
}
issue.setCustomFieldValue(approversField, approversList)
Alvin
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.
Sep 25, 2018

Hi @Ivan Tovbin , Thank you for a quick turn-around , I already have a script but will definitely try this one. Thanks! I think you might help me with my another question about hashmap, multikeymaps. I have raised another question here in the community

Ivan Tovbin
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.
Sep 25, 2018

I will definately take a look at your other question. In the meantime, please mark this solution as 'Accepted' if it resolved your issue.

Cheers!

Alvin
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.
Sep 25, 2018 • edited
2 votes
Jonathan Muse {Appfire}
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.
Sep 25, 2018

Hi, my name is Jonathan, I manage an app called Power Scripts. I see you already have the answer you're looking for but I thought I might share this in case anyone ready this is not a hardcore groovy developer who does not know off the top of their head which classes to import and how to maintain those classes during upgrades. Plus, I think it is a lot easier to read and write. I created this script, like most I write, in less than 5 minutes without doing any research first.

For example, instead of writing this line that does not even fit in the window:

CustomField approversField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("approversFieldName")

I can just call the field like this:

approver

Makes the script a lot easier when I can just call the fields by name. Anyway, here is the script:


struct grpApprv {
    string group;
    string approver;
}

grpApprv [] gas;

gas += {"group1", "Approver1"};
gas += {"group2", "Approver2"};

for(grpApprv ga in gas) {
    if(userInGroup(ga.group, currentUser())) {
        #{Approvers} += ga.approver;
    }
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events