Copy Custom field value from one user field to another

Avinash Gautam June 21, 2017

I am using a script post function to clone the Jira service Desk Ticket to Normal Jira Ticket when it moves from one state --> Another state

But the Service Desk Ticket contain one field i.e "Request participants" which is not present in normal jira ticket

Now my requirement is to copy this users from "Request participants" to "Participants" field in Jira ticket while cloning it.

 

I am using the below script to clone the issue and its working fine but i am not been able to copy "Request participants" to "Participants" field in my effort :

 

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.MutableIssue CustomFieldManager

customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField cf = customFieldManager.getCustomFieldObject(11100)

issue.summary = sourceIssue.getCustomFieldValue(cf).toString() + " : " + sourceIssue.getSummary()

issue.reporter = sourceIssue.getReporter()

issue.assignee = null

 

The above script is working but not able to copy the "Request participants". Please help me copy the field values.

 

I am trying to put the below lines into the code :

CustomField cf1 = customFieldManager.getCustomFieldObject(11101)

issue.participants = sourceIssue.getCustomFieldValue(cf1)

Ids : Request participants --- 11101

 

but it is not working . Please help me out with this issue. Thanks!

 

2 answers

1 accepted

1 vote
Answer accepted
Aidan Derossett _Adaptavist_
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.
June 28, 2017

Hey Avinash,

You would need to do something along the lines of:

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

//Grab necessary components
def issueManager = ComponentAccessor.getIssueManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def cfm = ComponentAccessor.getCustomFieldManager()

//Get the custom fields
def participants = cfm.getCustomFieldObjectByName("Participants")
def requestParticipants = cfm.getCustomFieldObjectByName("Request participants")

//Get the collection of users from Request Participants
def usersToCopy = issue.getCustomFieldValue(requestParticipants)
//Get the issue to copy over to
def targetIssue = issueManager.getIssueByCurrentKey("Target Issue Key")

def changeHolder = new DefaultIssueChangeHolder()

//Update Participants on target issue
participants.updateValue(null, targetIssue, new ModifiedValue(issue.getCustomFieldValue(participants), usersToCopy), changeHolder)

This script assumes that the issue being transitioned is the one from the support project. It takes the users from Request Participants and copies them over to a specified target issue under the Participants custom field.

In this script I just grab a target issue via an issue key. But you would not want to have to change the target key every time you intend on transitioning. So you'd also need to modify my script a little bit to use the key of the issue that you are copying over all of your values to. 

Let me know if that helps! :)                                                                 Aidan

 

0 votes
Aidan Derossett _Adaptavist_
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.
June 28, 2017

Hey Avinash,

So assuming that you are doing this in some sort of post-function, just to update a multi-user picker, you'd need to do something along the lines of:

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

//Grab necessary components
def issueManager = ComponentAccessor.getIssueManager()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def cfm = ComponentAccessor.getCustomFieldManager()

//Grab the custom fields
def participants = cfm.getCustomFieldObjectByName("Participants")
def requestParticipants = cfm.getCustomFieldObjectByName("Request participants")

//Get the users from Request Participants in the source issue (the issue being transitioned)
def usersToCopy = issue.getCustomFieldValue(requestParticipants)
//Get the target issue
def targetIssue = issueManager.getIssueByCurrentKey("Target Issue Key")

def changeHolder = new DefaultIssueChangeHolder()

//Update Participant's value in the target issue
participants.updateValue(null, targetIssue, new ModifiedValue(issue.getCustomFieldValue(participants), usersToCopy), changeHolder)

This script would take the user values from Request Participants on the issue that is being transitioned and copies them over to the specified target issue with the Participants custom field.

Let me know if you need help implementing this or if you have any other questions! :)

Hope that works!                                                                                                   Aidan                            

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events