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!
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)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.