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

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

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

3 answers

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

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

0 votes

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.

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

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

Yes, it is a multi-user picker.

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

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)

 

 HI @Mark Markov,

 

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

Is this possible?

Thanks

Swarna

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)

 

@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

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

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

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

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