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

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

Suggest an answer

Log in or Sign up to answer
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.
September 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.
September 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.
September 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.
September 25, 2018
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.
September 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;
    }
}
TAGS
AUG Leaders

Atlassian Community Events