Using Scriptrunner postfunction to set assignee based on custom user picker

Hi,

I want to set the assignee based on a customfield user picker (single) in a postfunction. The custom field can have selected a different user in different issues. 

The following script worked 1 time and successfully assigned the issue to the selected user in the customfield user picker: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cField = customFieldManager.getCustomFieldObject("customfield_10601") as String
issue.assigneeId = ComponentAccessor.userManager.getUserByKey(cField).key

The problem now is that it always assigns the issue to the user that I have selected for the first time in the customfield. It doesn't matter if I change the value of the customfield or if I create a new issue and select another user in the customfield... it always assigns the issue to the same first user. 

Does someone know why? Would be great if someone knows an answer, thank you!

 

5 answers

1 accepted

Hi,

 

Can I ask why you don't want to use the assignee user picker? Why do you need a customer user picker to do the same thing?

 

Also, which version of Jira and Scriptrunner are you using?

 

Thanks,

Johnson Howard

Hi Johnson,

thank you very much for you response and of course you can :) 

So the ticket is used to develop for example a new function in our erp. During the development process the ticket goes through different steps like describing the requirement, developing the code, code review and testing. Each of these steps are done by different persons. To ensure that nobody has too much work and to plan the resources I want to define who is doing which step. And the assigning of the issue to the responsible person should be automatically done in the transition from one step to the another one. Therefore I want to add e.g. 4 different user pickers, where I can define who is doing which step in the create screen. 

I am using Scriptrunner 5.0.1 and JIRA 7.3.0.

 
I changed my code to the following:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cField = customFieldManager.getCustomFieldObject("customfield_10601")
def username = issue.getCustomFieldValue(cField) as String
issue.assigneeId = ComponentAccessor.userManager.getUserByKey(username).key


In this code I get back "testuser(testuser)" for "username", so it shows 2 times the username and I also get the following error message:

 

2017-05-09 12:21:06,361 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-05-09 12:21:06,362 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TES-9, actionId: 11, file: <inline script>
java.lang.NullPointerException: Cannot get property 'key' on null object
at Script43.run(Script43.groovy:10)


I dont know why I get back the username twice and also have no other idea how to get the username another way :/

Thank you and kind regards,
Florian

I fully understand now. 

So how far have you go with the script, is just setting the assignee based on the user picker custom field that you need help with? 

Are you using a listener and checking the change history for the tranistion or are you using a post-function in each transition stage and just tweaking the code?

Hi Johnson,

yeah, the only thing I need help with is setting the assignee based on the user picker custom field. Thats the first script that gives me headache because I dont find a solution....

I want to use a postfunction so I can define on which transition the assignee will be changed to the custom picker user.

Thank you and kind regards,
Florian

Ok, give this a go, it seems to work for me:

ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
UserManager userManager =   ComponentAccessor.getUserManager();
issue.setAssignee((user))

I have the same requirement [Jira 7.3.1] and get the following error with the same code. I'm trying to update the assignee with the value in a custom field (which is a single-user picker).

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Proposed Team Head")
ApplicationUser user = getCustomFieldValue(cf) as ApplicationUser
UserManager userManager =   ComponentAccessor.getUserManager();
issue.setAssignee((user))

Error:

 

2017-05-15 19:21:03,002 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: STAR-515, actionId: 101, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField) values: [Proposed Team Head]
 at Script255.run(Script255.groovy:20)

 Any help would be greatly appreciated, spent ages trying to sort this out!

I get this same error. Was a fix ever found?

Hi Edward, 

Please see the script I have put below.

If this doesn't help then put your script on here and I will check it out.
The problem line for the above error was this:

ApplicationUser user = getCustomFieldValue(cf) as ApplicationUser

 It should be:

ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser

Thanks

Johnson

Hi Johnson,

thank you really, really much!!! Works fine! You Adaptavist guys really deliver the best support like always!

Here is the final code if someone has the same requirement:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cField = customFieldManager.getCustomFieldObject("customfield_10601")
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
UserManager userManager =   ComponentAccessor.getUserManager();
issue.setAssignee((user))

Thank you and kind regards,
Florian

Hi Florian,

 

Is it possible for you to accept my answer. It looks like you have accepted your own.

Yeah of course , sorry for that ;D

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,151 views 0 8
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