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

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

3 answers

1 accepted

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.

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

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

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.

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

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

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.

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

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

280 views 3 11
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you