Set assignee based on custom field value

Bart Meerveld
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.
October 25, 2016

Hi,

I want to add a postfunction script that sets the assignee based on a custom fields value.

So for example when field a has value 1 then it should be assigned to person 1. If it's value 2 then it should be person 2. We do have scriptrunner, can anybody tell me how the script should look to enable this?

3 answers

1 accepted

3 votes
Answer accepted
Vasiliy Zverev
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.
October 25, 2016

Here it is. It could be some changed based on custion field type.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

String userName;

switch(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldname").toString()){
    case "1": userName = "user1";break;
    case "2": userName = "user2";break;
    case "3": userName = "user3";break;    
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))
Bart Meerveld
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.
November 2, 2016

Thanks Vasiliy! Greatly appreciated!

 

Thomas Balasch July 28, 2017

Hi,

I have a similiar task to perform, but instead of the assignee I want to set the UserPicker-field, which we use for defining the approver.

Therefor the script should only be valid for our two workflows, which have approval-steps.

So if our employees enter their department, the manager should be set automatically for the following approval.

Best regards, Thomas

Hector Bermudez Castro November 20, 2017

Hi @Vasiliy Zverev,

 

I have the exact same problem to solve, but when I used your corde in a Jython post-function script I get the following error:

 

SyntaxError: ("mismatched input 'userName' expecting NEWLINE", ('<string>', 4, 7, 'String userName;\n'))

It seems that you have tried to perform an illegal workflow operation.

 

Do you have any idea why this is happening?

My code so far is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

String userName;

switch(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field").toString()){
case "A": userName = "User A";break;
case "B": userName = "User B";break;
case "C": userName = "User C";break;
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))


where "Custom Field" is a custom field defined by a single-choice select-list. 

 

Thanks in advance!

Best regards,

Hector

John Fernandes January 4, 2018

Hello Hector, 

You can try this script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

String user;

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("Select List Field");

switch(issue.getCustomFieldValue(cf) as String){
case "1": user = "username1";break;
case "2": user = "username2";break;
case "3": user = "username3";break;
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))
Gavin Minnis October 1, 2018

Is it possible to set the assignee based on a cascading custom field? I would like to have a condition that checks the first and second value of a cascading list and assigns a user based on both values.

For example:

1) Product A / Component B = user1

2) Product B / Component E = user2

Can someone help me with a scriptrunner script for this?

Akbarsait February 12, 2019

@Gavin Minnis Did you get a solution for Your query to handle cascading fields selection?

0 votes
Claudia Armentano January 4, 2019

Hi Experts,

I am trying the script suggested above by Vasiliy. however my issues remain unassigned.

I am putting the script on Create transition of the workflow.

Also i want to do this for a cascading field, how can I acheive that?

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

String userName;

switch(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Combo Onestack").toString()){
case "Jira + Confluence + Bitbucket + Add-ons": userName = "Seba.Sosa";break;
case "Jira + Confluence + Add-ons": userName = "smittal";break;
case "Jira + Add-ons": userName = "cprado";break;
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))

 

Thanks in anticipation.

Sakshi

Suggest an answer

Log in or Sign up to answer