Set Assignee based on custom select list value in post function using Script Runner?

Katie Weiss July 27, 2016

I have a Custom Field that is a select list.  When an issue is created, I would like the issue to be assigned to a particular user in a script runner post-function based on the value of that custom field selected on the Create Screen.  I have tried a million different things, and nothing seems to work - the issue always gets assigned to the Project Lead.  Can anyone point out what I'm doing wrong?

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def taskTypeField = customFieldManager.getCustomFieldObjectByName("Task Type")
def taskType = issue.getCustomFieldValue(taskTypeField).toString()

def project = issue.getProjectObject()
def lead = project.getProjectLead()
def reporter = issue.getReporter()

if(taskType == "Self-Directed")
{
issue.setAssignee(reporter)
}

else
{
issue.setAssignee(lead)
}

4 answers

1 accepted

0 votes
Answer accepted
Petar Petrov (Appfire)
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.
July 27, 2016

getCustomFieldValue returns an instance of Option for select lists. Try:

def taskType = issue.getCustomFieldValue(taskTypeField)?.value

Also, you can enable logging in order to debug your scripts - logging the value of taskType would help in this case.

Katie Weiss July 27, 2016

I did try that - one of the many things I tried wink to no avail.  BUT! I didn't have the logging on to debug my scripts, so I am definitely going to try that out and see if I can figure out what's going on there...

Katie Weiss July 27, 2016

OK - this is getting even more bizarre...  So, I put in the logging support as you suggested - thank you btw - SUPER helpful and I searched everywhere how to do that and finally you pointed me to a simple implementation - thank you!

So, I'm definitely getting the correct value of the select list - I'm getting the project lead and reporter - and I'm even going into the correct branch of the if statement based on the select list value!

So, what seems to be happening is that the setAssignee step is not working - BAH!  How do I fix that??

Petar Petrov (Appfire)
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.
July 27, 2016

You need to call issue.store() after setting the assignee. This, however, is not recommended anymore - the "proper" way to do it is via using IssueService.validateUpdate() and update() methods.

Katie Weiss July 27, 2016

Interesting...  That's odd, because I have a different script that does the exact same thing - simply issue.setAssignee and it appears to work...  I'll try it though and see...

Katie Weiss July 27, 2016

issue.store() worked - thank you!!!  laugh

Petar Petrov (Appfire)
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.
July 27, 2016

Cool, glad to help! The javadoc for MutableIssue states that:

After calling any 'setter' method, you will need to call store() to persist the change to the database. Note that this is a 'shallow' store - only fields directly part of the issue (in the database schema) are persisted.

0 votes
Gray Watson June 28, 2023

This worked for me:

//
// Script which sets the assignee in a Service-Now Jira workflow from a single-pick Approver
// custom field: "User Picker (single user)"
//

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()

// get our custom field Approver, error checking needed here
CustomField field = customFieldManager.getCustomFieldObjectsByName("Approver").iterator().next()
ApplicationUser user = (ApplicationUser)issue.getCustomFieldValue(field);

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// set the assignee and update the issue as the logged-in-user
issue.setAssignee(user)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
0 votes
Katie Weiss July 27, 2016

OK - this is getting even more bizarre...  So, I put in the logging support as you suggested - thank you btw - SUPER helpful and I searched everywhere how to do that and finally you pointed me to a simple implementation - thank you!

So, I'm definitely getting the correct value of the select list - I'm getting the project lead and reporter - and I'm even going into the correct branch of the if statement based on the select list value!

So, what seems to be happening is that the setAssignee step is not working - BAH!  How do I fix that??

0 votes
Deleted user July 27, 2016

Do we have an add on to achieve this instead of writing a script?

Suggest an answer

Log in or Sign up to answer