Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

2 answers

1 accepted

1 vote
Answer accepted
brbojorque Community Leader Dec 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.")
}

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

Laurie

0 votes

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.

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

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

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,375 views 22 32
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you