It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Jira Post Function update single user select field

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

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)

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...

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())

This one didn't seem to make a difference.

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)

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you