Jira Post Function update single user select field

Matt Wilson May 8, 2018

Jira 7.2.7

Script Runner 5.3.9

I've got a requirement to update a custom user selection field value with a set userid when an issue is cloned.  I've figured out the detecting a clone part (a different post helped me out on that).  My issue right now is that I can't get this field to update.  It appears to be changed, but the value I submit does not get written to the database.

Right now I've got a very dumbed down script that look like this

def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("mwilson")

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11500");
issue.setCustomFieldValue(cf, user)

 

This generates no errors, but appears to do nothing.  "customfield_11500" is a single user selection field.

If I modify the code like this to just update a custom single line text field, it works fine.

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11502");
issue.setCustomFieldValue(cf, "mwilson")

 

So obviously something special is required here for a user type field.  Any ideas what I'm going wrong here?

3 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 8, 2018

Kindly have a look at my article:

https://community.atlassian.com/t5/Agile-articles/Three-ways-to-update-an-issue-in-Jira-Java-Api/ba-p/736585

You should add something like this to the end of your script:

ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Matt Wilson May 8, 2018

First off, nice article.  Thank you for sharing it!

Your suggestion worked!  I had to make one other change though.

I updated my code as you suggested (new lines in bold)

def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("mwilson")
def lUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11500");
issue.setCustomFieldValue(cf, user)
ComponentAccessor.getIssueManager().updateIssue(lUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

I also had to move my post script function down so it fired after the "Creates the issue originally" step.  I see this does give me multiple updates in my ticket history, but I'm not concerned about that...

0 votes
Paul Stahlke January 2, 2020

None of the above methods worked for me, perhaps because I'm on a newer version of Jira Server 8.5.1. This worked for me setting a single user field called "Approver" with the current user.

import com.atlassian.jira.component.ComponentAccessor
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find { it.name == "Approver" }
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setCustomFieldValue(customField, currentUser)
0 votes
Praveen May 8, 2018

Hi Matt,

If you are ok with just the name of the user, try converting the user value to string like, 

issue.setCustomFieldValue(cf, user.toString())
Matt Wilson May 8, 2018

This one didn't seem to make a difference.

Suggest an answer

Log in or Sign up to answer