Set assignee based on custom field value and user picker field using script runner

lececere December 26, 2019

I am looking to set the assignee of an issue based on 2 fields, where the 2nd field is a user picker field. 

In plain English, here's what I'm looking to do:

  • If FieldA = Apple, then set assignee to value in user picker field called "Apple Picker"
  • If FieldA = Blueberry, then set assignee to value in user picker field called "Blueberry Picker"
  • If FieldA = Cherry, then set assignee to value in user picker field called "Cherry Picker"

Here's the code I've strung together from other posts. I'd love some help.

 

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

String user;

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");

def applepicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Apple Picker")
def assignee1 = issue.getCustomFieldValue(applepicker)

def blueberrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("BlueberryPicker")
def assignee2 = issue.getCustomFieldValue(blueberrypicker)

def cherrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Cherry Picker")
def assignee3 = issue.getCustomFieldValue(cherrypicker)

switch(issue.getCustomFieldValue(cf) as String){
case "Apple": user = assignee1;break;
case "Blueberry": user = assignee2;break;
case "Cherry": user = assignee3;break;
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))

3 answers

1 accepted

2 votes
Answer accepted
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2019

Hi @lececere ,

Please try the script below, I've simplified the variables and remove the redundant variable assignment.

The last part is to actually update the current issue for the script to work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");
def applepicker= cfManager.getCustomFieldObjectByName("Apple Picker")
def blueberrypicker= cfManager.getCustomFieldObjectByName("Blueberry Picker")
def cherrypicker= cfManager.getCustomFieldObjectByName("Cherry Picker")

def user
switch(issue.getCustomFieldValue(cf)){
case "Apple":
user = issue.getCustomFieldValue(applepicker);
break;
case "Blueberry":
user = issue.getCustomFieldValue(blueberrypicker);
break;
case "Cherry":
user = issue.getCustomFieldValue(cherrypicker);
break;
}

try{
issue.setAssignee(user)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}catch(Exception ex){
log.debug("The issue was not updated.")
}
lececere December 30, 2019

Fabulous! That worked like a charm @brbojorque !  I appreciate your help! 

Laurie

Amine Tanboura October 18, 2022

Hello @brbojorque  ,

I have an error message with issue.setAssignee(user)

Jira server version 7.13.1 

Thank you in advance.

Amine.

Scriptrunner error.png

0 votes
Amine Tanboura October 18, 2022

Hello @Nic Brough -Adaptavist- ,

I tested your script and it's working, but I wanted to ask about what if you have only one condition so we don't need the switch.

Then how the code should be in this case.

Thank you in advance.

Regards,

Amine.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 18, 2022

If you only have one thing, then leave out all the switch stuff, just run the one line you need 

In the code above, just 

user = issue.getCustomFieldValue(applepicker); 
Like Amine Tanboura likes this
Amine Tanboura October 19, 2022

What about the condition where I should put it 

Here is my code and it's not working with one case

Here is my code :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
String userName;
def csProduct = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field")
switch(issue.getCustomFieldValue(csProduct)){
case "value1": userName = "user1" ;break;
//only one condition 

}
log.error(userName)
log.error(issue.getCustomFieldValue(csProduct));
issue.setAssignee(ComponentAccessor.getUserManager().getUserByName(userName))

 

Thank you,

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2019

The "as String" inside the switch statement is either pointless or going to fail to do what you want.

If FieldA is a text field holding a string, you don't need to convert it to a string.  If FieldA is not a text field, then you're going to get a representation of the object it contains, not a simple string with the name of a fruit in it.

Assuming FieldA is a select list, for example, then it contains an "option" object, so you'll want to use .getValue() to get the name of the option.

lececere December 26, 2019

Thanks for your quick response @Nic Brough -Adaptavist- 

That makes sense. I've removed the string conversion. My updated code looks like this:

 

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

String user;

def cfManager = ComponentAccessor.getCustomFieldManager();
def cf = cfManager.getCustomFieldObjectByName("FieldA");

def applepicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Apple Picker")
def assignee1 = issue.getCustomFieldValue(applepicker)

def blueberrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("BlueberryPicker")
def assignee2 = issue.getCustomFieldValue(blueberrypicker)

def cherrypicker= ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Cherry Picker")
def assignee3 = issue.getCustomFieldValue(cherrypicker)

switch(issue.getCustomFieldValue(cf)){
case "Apple": user = assignee1;break;
case "Blueberry": user = assignee2;break;
case "Cherry": user = assignee3;break;
}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))

 

Is there anything else I need to do to this to make it work? It isn't.

THanks,
Laurie

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2019

Is FieldA really a text field?

Also, I would simplify - the "user" object should be a jira user object, not a string, then you can just use issue.setAssignee(user) without having to cast it from the assigneeX and then look it up.

Suggest an answer

Log in or Sign up to answer