Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to update user custom field value

Madhusudhan Matrubai
Contributor
February 20, 2018

Hi There,

I'm trying to update custom field of type "User" and somehow it's not working for me and no errors either.

import com.atlassian.jira.ComponentManager 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.CustomField

CustomField itManager = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("IT Manager")
ApplicationUser itManagerUser = ComponentAccessor.getUserManager().getUserByName('popeye')

issue.setCustomFieldValue(itManager, itManagerUser);

 

2 answers

0 votes
Madhusudhan Matrubai
Contributor
February 21, 2018

I have found the workaround, just in case someone is looking for a similar solution. 


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def changeHolder = new DefaultIssueChangeHolder()

def itMgrField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("IT Manager")
def itMgrTargetFieldValue = issue.getCustomFieldValue(itMgrField)

ApplicationUser itMgrSourceFieldValue = ComponentAccessor.getUserManager().getUserByName('popeye')
itMgrField.updateValue(null, issue, new ModifiedValue(itMgrTargetFieldValue, itMgrSourceFieldValue),changeHolder)
0 votes
Ivan Tovbin
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.
February 20, 2018

Perhaps this is because setCustomFieldValue does NOT write your changes to the database and thus you have to update your issue after calling this method. 

Try adding this to your code:

import com.atlassian.jira.issue.UpdateIssueRequest

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, UpdateIssueRequest.builder().build())
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.
February 20, 2018

Or put the post function first in the list of post functions

Madhusudhan Matrubai
Contributor
February 21, 2018

Thanks @Ivan Tovbin and @Alexey Matveev for your prompt help here.

Unfortunately the above suggested workaround did not help me.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events