Copy Custom field value from one user field to another

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

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

 

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

355 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you