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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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:
Problem is that the user has not been set correctly as no hyperlink is shown towards the user in the Search Issue screen:
When 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:
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:
AFTER:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am having the same experience. No error, just unassigned sub-tasks. Any resolution yet?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.