How to copy the current user to custom field using ScriptRunner

Leslie Baker June 16, 2021

During a workflow transition I'm attempting to copy the value of the current assignee into a single user custom field, then separately add the current user (the person clicking the button that  does the transition) into another single user customfield. 

Please can someone help me write the Groovy script for Scriptrunner?

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 16, 2021

Hi @Leslie Baker

For your requirement, you can try out something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def issue = issue as MutableIssue
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def assignee = issue.assignee

def userPicker = customFieldManager.getCustomFieldObjectsByName("User Picker")[0]
def currentUser = customFieldManager.getCustomFieldObjectsByName("Current User")[0]

issue.setCustomFieldValue(userPicker, assignee)
issue.setCustomFieldValue(currentUser, loggedInUser)

 Below are a few test screens for your reference:-

You will need to use the Custom Script option in the Post-Function as shown below:-

config.png

1) First when the ticket is created only basic info is entered and the assignee is selected as shown below:-

image1.png

2) Once the ticket has been created, only the Report and Assignee are visible as shown below:-

image2.png

3) Next, when the issue is transitioned to In Progress, the additional single user picker fields are displayed as shown below:-

image3.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Leslie Baker June 17, 2021

Thanks so much, that's done the trick.

Leslie Baker June 17, 2021

only tweak was the variable name for issue to iss as it confuses with the other 'issue' but otherwise spot on!

Suggest an answer

Log in or Sign up to answer