Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

On transition, set assignee based on user in custom field existing in a specific user group

Stuart_Hart March 3, 2020

Hi,

I'm trying to implement some automation on a transition in a workflow. In the transition, I would like for the Issue to be reassigned if the value in a custom field (User Picker (single user)) exists in a specific User Group.

In 'plain English' the script would be something like this:

if (Ticket Owner value exists in "User Group A") {

Assignee = "Specific User A";

} else if (Ticket Owner value exists in "User Group B") {

Assignee = "Specific User B";

}

I'm currently using the Precondition: User Is In Any Groups (JSU) post function, which kinda does what I want, only I don't want it checking against the current user executing the transition; this will cause problems in our work processes in the future.

I believe Script Runner can be used to achieve what I want. I've been trying to use the examples on this page but I haven't been able to achieve much.

1 answer

0 votes
PD Sheehan
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.
March 3, 2020

Hi @Stuart_Hart 

In the scriptrunner world, you will be looking at a custom post function, not behaviors/conditions.

They have many built-in functions, but none that does exactly what you want. So you'll need to create it from scratch.

Something like this may work for most cases:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def crowdService = ComponentAccessor.crowdService
def userManager = ComponentAccessor.userManager

def groupMapping = [
   'User Group A': 'username of specific user a',
   'User Group B': 'username of specific user B',
]

def ownerCfs = customFieldManager.getCustomFieldObjectsByName('Ticket Owner')
if(!ownerCfs) return null //collection is empty, the custom field doesn't exist with that name

def owner = issue.getCustomFieldValue(ownerCf.first())

if(owner && owner.active){
groupMapping.find{ group, assigneeUsername ->
if(crowdService.isUserMemberOfGroup(owner.name, group)){
def assignee = userManager.getUserByName(assigneeUsername)
issueInputParameters.setAssigneeId(assignee.Id.toString() )
return true
}
}
} else {
// perhaps you have a default assignee when the ticket owner doesn't exist or is inactive?
}

Rather than a simple if then else, I created a map of group:username.

This way, you can expand your cases without having to expand the logical portion of the script. The first group with the ticket owner will be found and the corresponding user assigned.

Suggest an answer

Log in or Sign up to answer