Workflow post function to copy custom field to assignee

flaimo February 26, 2016

I want to use a custom script as a scriptrunner transition post function to set the assignee based on the value of a custom user field ("Implemented by"):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser

def userUtil                          = ComponentAccessor.getUserUtil();
UserManager userManager               = ComponentAccessor.getUserManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfImplementedByField      = customFieldManager.getCustomFieldObject('customfield_11142')
def cfImplementedByValue              = issue.getCustomFieldValue(cfImplementedByField)
ApplicationUser implementedBy         = userManager.getUserByName(cfImplementedByValue?.displayName)

if (implementedBy != "" && implementedBy != null) {
    issue.setAssignee(implementedBy)
}

I get the following error:

Screen Shot 2016-02-26 at 14.41.09.png

How to I get to recognize the script, that the custom field value is a user?

4 answers

1 accepted

1 vote
Answer accepted
Kristian Walker _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.
February 26, 2016

Hi Flamio,

I have attached a sample script which take the value from a custom user picker such as one called Implemented By and set it to the Assignee field during a transition.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

// Get required Managers
def customFieldManager = ComponentAccessor.getCustomFieldManager()

// get a pointer to the Implemented By user picker field and its value
def userCf = customFieldManager.getCustomFieldObjectByName("Implemented By")
def implUser = issue.getCustomFieldValue(userCf) as ApplicationUser

// If the Implemented By Field is not null then set the assignee
if(implUser){
	issue.setAssigneeId(implUser.username.toString())
}

I hope this helps.

Thanks

Kristian

 

 

Kristian Walker _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.
February 29, 2016

Hi Flamio,

If this answer is now working as expected could you please accept it so that users who have a similar query can easily find the solution.

Thanks

Kristian

0 votes
Pieterjan Vanpee June 6, 2017

Hello,

 

I'm using your code defined as an additional action on the "Clones an issue and links" built-in script (Scriptrunner for JIRA), as can be seen in below screenshot:

image.png

In the main Story we are defining the "8-LP-Assignee" User Picker field during one of the transitions. The above mentioned script is being defined as a Post-Function in this same transition, which is cloning the issue into another project.

Result is the following:

- For some users, the assignee of the cloned issue has been set correctly towards the person defined earlier in the "8-LP-Assignee" field.

- For other users, the value of the assignee field is being showed as the username in the View issue screen, as e.g. below:

image.png

Problem is that the user has not been set correctly as no hyperlink is shown towards the user in the Search Issue screen:

image.pngWhen I click on the assignee field and directly click on enter, the real person is getting defined, as apparently now JIRA is going to lookup the correct person with as a result that the real name is being shown instead of the username:

image.png

image.png

By using the Script Console, you can see what JIRA is defining in the issue.assignee field, BEFORE and AFTER selecting the assignee field manually and clicking on enter as explained above:

BEFORE:

image.png

AFTER:

image.png

Remark the difference of capital and small letters as username between the brackets.

 

Can anyone help me out here in order to solve this problem?

 

Thanks a lot.

 

0 votes
Cinnober Admins May 31, 2017

I'm having difficulty with the same issue... I can't get the script suggested above to work, though maybe there is some deprecated code? There are no errors in ScriptRunner, it just isn't working properly. 

Edward Greathouse August 10, 2018

I am having the same experience. No error, just unassigned sub-tasks. Any resolution yet?

Cinnober Admins August 12, 2018

No idea what issue I had before (I don't remember), but it looks like I went with something following the pattern of the example code above. 

Could have been that the post-function was placed before "Creates the issue originally." or something

0 votes
flaimo February 26, 2016

Thank you, your solution works. There is some strange effect on a Kanban board though. When a user drag&drops an issue into a column, which triggers the transition with the post function script, the assignee is set, but the user avatar is not displayed on the board. Even a refresh doesn't help. Do I need to trigger some sort of manual refresh here somehow?

Kristian Walker _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.
February 28, 2016

Hi Flamio,

I have looked at this and noticed the line in the if should be changed to issue.setAssigneeId(implUser.username.toString()) as the setAssigneeId method expects the username of the user.

I have updated the example code above. This new code should set the avatar on the Kanban board.

I hope this helps.

Thanks

Kristian

flaimo February 29, 2016

That did it. Thank you very much!

Suggest an answer

Log in or Sign up to answer