Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Copy field value from linked issue

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants

// the name of the field to copy
final String fieldNameToCopy = "Priority"

// leave blank to copy from the last linked issue (regardless the link type)
final String issueLinkTypeName = "Blocks"

def fieldManager = ComponentAccessor.fieldManager

def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }
if (!fieldToCopy) {
    log.info "Could not find field with name $fieldNameToCopy"
    return
}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id)
if (!linkedIssues) {
    log.info "There are no linked issues"
    return
}

if (issueLinkTypeName && !(issueLinkTypeName in linkedIssues*.issueLinkType*.name)) {
    log.info "Could not find any issue, linked with the $issueLinkTypeName issue type"
    return
}

def linkedIssue = issueLinkTypeName ?
    linkedIssues.findAll { it.issueLinkType.name == issueLinkTypeName }.last().destinationObject :
    linkedIssues.last().destinationObject

def fieldToCopyId = fieldToCopy.id

switch (fieldToCopyId) {
    case fieldManager.&isCustomFieldId:
        def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(fieldToCopyId)
        def linkedIssueCustomFieldValue = linkedIssue.getCustomFieldValue(customField)
        issue.setCustomFieldValue(customField, linkedIssueCustomFieldValue)
        break

    case IssueFieldConstants.COMPONENTS:
        def commonComponents = linkedIssue.components.findAll { it.name in issue.components*.name }
        issue.setComponent(commonComponents)
        break

    case IssueFieldConstants.FIX_FOR_VERSIONS:
        def commonFixedVersions = linkedIssue.fixVersions.findAll { it.name in issue.fixVersions*.name }
        issue.setFixVersions(commonFixedVersions)
        break

    case IssueFieldConstants.AFFECTED_VERSIONS:
        def commonVersions = linkedIssue.affectedVersions.findAll { it.name in issue.affectedVersions*.name }
        issue.setFixVersions(commonVersions)
        break

    default:
        issue[fieldToCopyId] = linkedIssue[fieldToCopyId]
}
Having an issue with this script?
Report it here