Forums

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

Update Custom User field with current user/LoggedInuser

Norma Pena November 18, 2022
Custom Field does not update with user performing the transition 
Custom Post function script running 
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption


def authContext = ComponentAccessor.getJiraAuthenticationContext()

def currentUser = authContext.getLoggedInUser()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def userManager = ComponentAccessor.getUserManager()

def myCustomfield = customFieldManager.getCustomFieldObjectsByName('Approved By').first()

issue.setCustomFieldValue(myCustomfield, currentUser)


ComponentAccessor
.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

6 answers

1 accepted

0 votes
Answer accepted
Norma Pena December 28, 2022

Figured it out!

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.user.ApplicationUser
def customFieldManager = ComponentAccessor.getCustomFieldManager() def userManager = ComponentAccessor.getUserManager()
// Get the current user def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the custom field def customField = customFieldManager.getCustomFieldObjectByName('Approved By') // Get the current value of the custom field def currentUsers = issue.getCustomFieldValue(customField) as List<ApplicationUser> // Set the value of the custom field if (currentUsers == null) { currentUsers=[currentUser] } if (currentUser != null) { currentUsers.add(currentUser) } // Update the value of the custom field issue.setCustomFieldValue(customField, currentUsers)

 

0 votes
Norma Pena December 28, 2022
The custom field is a multi-user picker. Is there a way for the field to be updated with the new user each time the transition takes place? Currently, it clears field and updates each time a new user transitions the ticket. 
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def userManager = ComponentAccessor.getUserManager()

// Get the current user

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get the custom field

def customField = customFieldManager.getCustomFieldObjectByName('Approved By')

// Update the value of the custom field

issue.setCustomFieldValue(customField, [currentUser])
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 18, 2022

Hi @Norma Pena

Can you please clarify what field you use for the Approved By? Is it a single-user picker, a multi-user picker or maybe a text field?

I can get this to work in my environment without issues using a code similar to yours and testing with a single-user picker.

Please note that the sample codes below are not 100% exact to your environment. Hence you will need to make the required modifications.

Below is the code that I have tested for the single-user picker:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def approver = customFieldManager.getCustomFieldObjectsByName('Approved By').first()

issue.setCustomFieldValue(approver, loggedInUser)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Below are a few test screenshots for your reference:-

1. When the issue is first created, the Approver By field is not set:-

test1.png

2. Once the issue has transitioned to the In Progress status, the Approved By field is updated as shown below:-

test2.png

If it is a multi-user picker, you need to try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issue = issue as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.getGroupManager()

def group = groupManager.getGroup('jira-administrators') // set group name
def usersInGroup = groupManager.getUsersInGroup(group)
def approvers = customFieldManager.getCustomFieldObjectsByName('Approved By Multi').first()
approvers.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approvers), usersInGroup), new DefaultIssueChangeHolder())

 

When using the multi-user picker, when the issue transitions, the field will be updated like:-

test3.png

 

If you are still encountering an error, it would be helpful if you could share some of your log output for further investigation.

 

I hope this helps to answer your question. :-)

 

Thank you and Kind regards,
Ram
 

0 votes
Alex Koxaras _Relational_
Community Champion
November 18, 2022

Hi @Norma Pena 

Try this and replace my field's name with yours. I've tried it on scriptrunner's console and works fine:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager

IssueManager issueManager = ComponentAccessor.getIssueManager()
def cfObj = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Test User Picker (single)")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setCustomFieldValue(cfObj, user)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED ,false)
0 votes
Tim Perrault
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.
November 18, 2022

Hi @Norma Pena 

 

You could try this:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// a user custom field

def userCf = customFieldManager.getCustomFieldObjectByName("Approved By")

def currenrUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setCustomFieldValue(userCf, currenrUser)

ComponentAccessor.getIssueManager().updateIssue(currenrUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
0 votes
Norma Pena November 18, 2022

@Ram Kumar Aravindakshan _Adaptavist_ any chance you could take a look?

Suggest an answer

Log in or Sign up to answer