Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,296,708
Community Members
 
Community Events
165
Community Groups

Scripted Field to copy custom field from Linked Issues

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

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

}

Hi,

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

best regards 

@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?

Hi @Jiri Kanicky 

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

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.

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?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

328 views 0 10
Join discussion

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