Set assignee based on custom field value

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?

1 answer

1 accepted

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))

Thanks Vasiliy! Greatly appreciated!

 

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

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

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))

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,331 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot