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?

2 answers

1 accepted

3 votes
Accepted answer

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

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 N I'm New Here Tuesday

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

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
Community showcase
Published Wednesday in Jira

Make your Atlassian Cloud products more secure: our NEW admin security guide

Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...

258 views 0 6
Read article

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