Copy table values via post-function
This Groovy script facilitates copying table values from a root issue to a target issue connected by a default link.
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
// Initialize Jira components
CustomFieldManager customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def slurper = new JsonSlurper()
// Define the custom field ID to work with
final Long CCOW_Field_ID = 11827
def ccowField = customFieldManager.getCustomFieldObject(CCOW_Field_ID)
// Get the current issue and user
def currentIssue = issue
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
// Log the current issue key
log.warn("Current issue: ${currentIssue.key}")
// Retrieve the custom field value for the current issue using the alternative method
def ccowValActual = ccowField.getCustomFieldType().getActualValue(ccowField, currentIssue)
// Log the value of the custom field for the current issue
log.warn("Custom field value for the current issue: ${ccowValActual}")// Find linked issues (e.g., "is duplicated by" link type)
def linkedIssues = ComponentAccessor.getIssueLinkManager().getInwardLinks(currentIssue.id)
linkedIssues.each { IssueLink link ->
def linkedIssue = link.sourceObject as Issue
log.warn("Copying custom field value to linked issue ${linkedIssue.key}") def currVal = ccowField.getCustomFieldType().getActualValue(ccowField, linkedIssue)
if (currVal) {
// Clear the existing value in the linked issue's custom field
def newValues = []
// Append values from the current issue's custom field to the linked issue's custom field
slurper.parseText(ccowValActual).get("values").each { row ->
row.remove('id')
newValues.add(row)
log.warn "old Val: ${row}"
}
def newFieldVal = [
values: newValues
]
// Set the custom field value in the linked issue, overwriting the existing value
linkedIssue.setCustomFieldValue(ccowField, JsonOutput.toJson(newFieldVal))
ComponentAccessor.getIssueManager().updateIssue(currentUser, linkedIssue, EventDispatchOption.ISSUE_UPDATED, false)
} else {
// Set the custom field value in the linked issue as before
linkedIssue.setCustomFieldValue(ccowField, ccowValActual)
ComponentAccessor.getIssueManager().updateIssue(currentUser, linkedIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
© 2023, STAGIL