Set and store assignee value

Filzah Aziz February 15, 2021

Hi all, I'm using the Scriptrunner post function and i'm trying to set the assignee based on a component and custom field. I understand apart from setting the assignee, you'd also need to store that in the DB. (Currently it doesn't change the value of the assignee). Could anyone help with how to store the assignee? Below is what i have so far

import com.atlassian.jira.component.ComponentAccessor

// the username of the assignee
final String user= "user1"

def user_user1 = ComponentAccessor.userManager.getUserByName(user)
def location = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("Location")
def location_val = issue.getCustomFieldValue(location)


if(issue.components*.name.contains('Hardware') && location_val == "Utrecht" ){
issue.setAssignee(user_user1)

}

2 answers

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2021

You do not need to think about storage or indexing, the post-functions on the transition will do all of that for you, as long as you put your post-function in the right order in the list (it should be done before indexing and store issue)

Your code looks mostly ok to me as well, but I suspect there's a fault in the "if".  You are comparing location_val with a string, but that will only work if location_val really is a string.  Is the location field really a short text field? 

Or is it a select list?  If it's a select list, then your "if" is comparing a string with an option, so they're never going to be equal.  I would try

&& "Utrecht".equals(location_val.getName()

If it is not that, then I'd guess that your user does not exist, or your feidls do not have the values you are checking for (or do not exist for the project and issue type)

Filzah Aziz February 16, 2021

You're spot on about the if statement. The was the issue as the field that i'm referring to is a select field. I tried your method but it kept giving me errors. I've added this line to get the value from a select field:

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Location'}
def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Utrecht' }

0 votes
Kristján Geir Mathiesen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 15, 2021

Hi @Filzah Aziz 

Have you taken a look at their library?  https://library.adaptavist.com/

HTH,
KGM

Suggest an answer

Log in or Sign up to answer