Workflow post function to copy custom field to assignee

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

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

 

 

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

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?

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

That did it. Thank you very much!

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. 

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.

 

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

285 views 0 12
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot