Looking to set assignee based on cf values using Scriptrunner

Gustone Lewis June 29, 2021

I'm looking to have a post function assign a JIRA issue depending on two fields that are of the single select type (Region and Network). Although the JIRA in question has both fields set with values that are described in the IF statement the user assigned is userB. Is there something I'm missing in the script below?

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;

//define custom field manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()

//define custom fields
def Network = customFieldManager.getCustomFieldObject("customfield_10742")
def Region = customFieldManager.getCustomFieldObject("customfield_14440")
def userA = ComponentAccessor.userManager.getUserByName("John")
def userB = ComponentAccessor.userManager.getUserByName("Maria")


if(Network == "Airport"){
issue.setAssignee(userA)
}
else{
issue.setAssignee(userB)
}

1 answer

1 accepted

1 vote
Answer accepted
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.
June 29, 2021

Your def Network and def Region lines are not doing what you think.  The variables will contain the objects representing the custom fields, not the content of them.

It's also quite likely that even if you do get the values, the if ( == ) bit won't work - if the fields are select lists, the content of the fields will be Jira "options" not strings.  If that's the case, then you'll want something more like

if ("Airport".equals(networks-value.getName() )

 

https://library.adaptavist.com/entity/update-priority-based-on-a-custom-field has some sample code about working with custom fields.

Gustone Lewis June 30, 2021

Nic, thanks for the explanation and link to examples it really helped! After applying the changes the assignee is being assigned to the correct user based on the contents of the designated custom fields. I'm still a bit green when it comes to creating custom scripts with ScriptRunner, is there a recommended resource you can point me to for learning the ins and outs of it?

Again, thanks for taking the time to answer this for me, truly appreciate it!

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.
June 30, 2021

I tend to steal a lot from the Library, even the scripts I've written that have made it in there!

But for getting started, I'd skim through the resources at https://docs.adaptavist.com/sr4js/latest

Like Gustone Lewis likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.80
TAGS
AUG Leaders

Atlassian Community Events