I need help copying the assignee field to a user picker custom field

Suzanne Seaton August 21, 2018

We want to copy the assignee field to a custom field named 'Responsible User(s)'. Thanks

3 answers

1 vote
Mark Markov
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.
August 22, 2018

Hello @Suzanne Seaton

I think you also need to save users that already exist in this field, so script should be like this

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Responsible User(s)")

List<ApplicationUser> userAlreadyInCustomField = issue.getCustomFieldValue(userCf)
userAlreadyInCustomField.add(issue.getAssignee())
issue.setCustomFieldValue(userCf, userAlreadyInCustomField)
ComponentAccessor.getIssueManager().updateIssue(issue.getAssignee(), issue, EventDispatchOption.ISSUE_UPDATED, false)

 

Swarna Radha August 25, 2018

 HI @Mark Markov,

 

I want to set the approver name in to a custom field B (mulitple user picker).

Is this possible?

Thanks

Swarna

Mark Markov
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.
August 28, 2018

Yes is's possible. You can use script above with some modification.

You need getCustomFieldValue() method istead of getAssignee()

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Responsible User(s)")
def fieldtoCopy = customFieldManager.getCustomFieldObjectByName("FromFieldName")

List<ApplicationUser> userAlreadyInCustomField = issue.getCustomFieldValue(userCf)
userAlreadyInCustomField.add(issue.getCustomFieldValue(fieldtoCopy))
issue.setCustomFieldValue(userCf, userAlreadyInCustomField)
ComponentAccessor.getIssueManager().updateIssue(issue.getAssignee(), issue, EventDispatchOption.ISSUE_UPDATED, false)

 

Suzanne Seaton August 28, 2018

@Mark Markov, thanks so much for trying to help me. Unfortunately I get the following error on line 10 which is your line that states 'List<Application User> userAlreadyInCustomField = issue.getCustomFieldValue(userCf)

 

Here is the error: cannot assign value of type java.lang.Object to variable of type java.util.List (ApplicationUser)

 

Thanks

Mark Markov
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.
August 28, 2018

This is static type checking error you can ignore it. It will work :)

Alvin
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.
October 1, 2018

Hi @Mark Markov , code does not working, it returns the error, I am trying to copy the current log in user to a single user pick custom field. How can I do that? Thanks

M November 15, 2019

Hi,

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

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()
def cfMultiSelectUser = customFieldManager.getCustomFieldObjectByName("CC")
def cfMultiSelectUserValue = issue.getCustomFieldValue(cfMultiSelectUser)

((java.util.List)cfMultiSelectUserValue).add(currentUser)
cfMultiSelectUser.updateValue(null, issue, new ModifiedValue(cfMultiSelectUserValue, cfMultiSelectUserValue),changeHolder);

 The above code should have worked.


Regards,
MG

0 votes
Nic Brough -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.
August 21, 2018

There's an example of setting lots of different field types over at https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/set-issue-attributes.html

On the section that sets the custom user-picker field, replace the "get user by name" function with a simple issue.getAssignee()

Although, I guess the field may be a multi-user picker.   Assuming you really want to add the assignee, not replace another or destroy the current list, then you will want to get the current value of the customfield (which will be a collection or list), add to it, and then poke the collection back into the field.

Suzanne Seaton August 21, 2018

Thanks Nic, this is what I tried, and it's not successful:

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userUtil = ComponentAccessor.getUserUtil()

// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Responsible User(s)")
issue.setCustomFieldValue(userCf, issue.get.Assignee())

Nic Brough -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.
August 21, 2018

Is the field a multi-user picker?  The name suggests it might be?

Suzanne Seaton August 21, 2018

Yes, it is a multi-user picker.

Nic Brough -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.
August 21, 2018

Ok, so the last paragraph applies, you need to treat it as a list.

0 votes
Chander Inguva
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.
August 21, 2018

This can be done using script runner or jira suite utilities plugins Suzanne.

Suzanne Seaton August 21, 2018

Yes, I plan to use script runner (I tagged issue with groovy-script-runner). I need help with my groovy script. I found similar posts where use case was to copy custom field to the assignee field. This is opposite... copy the assignee to custom field. Thanks

Suggest an answer

Log in or Sign up to answer