Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scripted Field to copy custom field from Linked Issues

Avinash March 1, 2018

I am trying to copy the value of a custom field from the linked issue. It works good with one to one link but doesnt work when they have the same issue linked to multiple issues. Example: Ab is the project where the scripted field is present, XY is a project where the value is present when Ab-1 and Ab-2 has link with XY-1 the script works for only Ab-1 but Ab-2 is null.  Any help appreciated 

image.png

 

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Carmen Creswell [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.
May 10, 2018

Here is a script that will copy the values from a Custom Field into the Linked Issues' Custom Fields:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issue = issueManager.getIssueByCurrentKey('JRA-2') // change this to match your main issue key
def customField = customFieldManager.getCustomFieldObjectsByName('MultiSelectA')[0] // change this to match your custom field name

def customFieldValue = issue.getCustomFieldValue(customField)
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()

def linkedIssues = issueLinkManager.getLinkCollection(issue, currentUser).getAllIssues()

for (int i = 0; i < linkedIssues.size(); i++) {
log.debug("Updating issue: ${linkedIssues[i]}")

// This is for the Multi Select List. It will change if you work with another type of Custom Field
// For example, you won't have to loop through a text field, you would just pass what was in customFieldValue
def values = customFieldValue.collect{ Option option ->
option.optionId.toString()
}

issueInputParameters.with {
addCustomFieldValue(customField.idAsLong, *values) // *values is something Groovy does to destruct a list into plain arguments
}

def updateValidationResult = issueService.validateUpdate(currentUser, linkedIssues[i].id, issueInputParameters)

if (updateValidationResult.isValid()) {
def finalUpdateResult = issueService.update(currentUser, updateValidationResult)
log.debug(finalUpdateResult.errorCollection)
log.debug(finalUpdateResult.warningCollection.getWarnings())
log.debug(finalUpdateResult.isValid())
} else {
log.debug(updateValidationResult.errorCollection)
log.debug(updateValidationResult.warningCollection.getWarnings())
}

}
Deleted user September 2, 2019

Hi,

the script works fine with multiselect fields, but how do I get it to work with Singe User Picker custom fields? 

best regards 

Anuradha Yadav September 7, 2022

Hi @[deleted] ,

Have you found the solution for your question?

Can come one please help?

 

Regards,

Anuradha

Anuradha Yadav September 11, 2022

Hi @Carmen Creswell [Adaptavist] 

Could you please help here?

 

Regards,

Anuradha

0 votes
Jiri Kanicky November 11, 2018

@Avinashhave you been able to create the script?

I also need a script which copies Region field value from linked issue to Region field of the issue where the link was created.

Can you share it?

Avinash November 11, 2018

Hi @Jiri Kanicky 

Yes, What type of field in JIRA is region field value ? 

Jiri Kanicky November 11, 2018

Hi. Its a Select List (single choice) custom field.

I have the following setup:

  • Project_Support
  • Project_Surveys

When I link Support ticket to Survey ticket (from Survey ticket), I need to pull the Support ticket Region field value and update the Survey ticket Region field value.

Thanks for your help.

0 votes
Kyle Moseley
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.
March 7, 2018

I think your only reference to issue (the current issue, AB-1 or AB-2) is where you're assigning linkedTasks's initial value. That leads me to believe that there could be some sort of flaw in the logic. Are you sure AB-2 has the same direction of link at AB-1?

TAGS
AUG Leaders

Atlassian Community Events