Hi all,
I have two asset object fields, called Field A and Field B.
Let’s say I added the value “Jira” to Field A, and later changed it to “Confluence.” Now, I want to store the original value, "Jira," in Field B after it was changed to "Confluence." Could you please suggest a solution?
I am using Data Center v10.3.7, and we have ScriptRunner for Jira. I’m using the listener below, but I’m not getting the intended result.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Logger
def log = Logger.getLogger("com.example.plugins")
// IDs of the relevant Asset object fields
def sourceFieldId = "customfield_XXXXX"
def targetFieldId = "customfield_XXXXX"
def issue = event.issue
def cfManager = ComponentAccessor.customFieldManager
def sourceCF = cfManager.getCustomFieldObject(sourceFieldId)
def targetCF = cfManager.getCustomFieldObject(targetFieldId)
if (!sourceCF || !targetCF) {
log.warn("Source or target custom field not found.")
return
}
// Use field name for change detection
def sourceCFName = sourceCF.name
def changeItem = event.getChangeLog()?.getRelated("ChildChangeItem")?.find {
it.field == sourceCFName && it.oldstring != it.newstring
}
if (!changeItem) {
log.info("No change detected for Field A (${sourceCFName}). Skipping update.")
return
}
def mutableIssue = issue as com.atlassian.jira.issue.MutableIssue
def selected = mutableIssue.getCustomFieldValue(sourceCF)
if (!selected) {
log.info("No value selected in Field A.")
return
}
// Handle single-select field: get the object directly
def sourceObject = (selected instanceof Collection) ? selected[0] : selected
if (!sourceObject) {
log.warn("Source object is null after extraction.")
return
}
// Copy the Asset object to the target field
mutableIssue.setCustomFieldValue(targetCF, [sourceObject])
// Persist the issue update (user context is managed by ScriptRunner listener configuration)
ComponentAccessor.issueManager.updateIssue(
ComponentAccessor.jiraAuthenticationContext.loggedInUser,
mutableIssue,
EventDispatchOption.DO_NOT_DISPATCH,
false
)
if (sourceObject instanceof com.riadalabs.jira.plugins.insight.services.model.ObjectBean) {
log.info("Copied asset object '${sourceObject.name}' from Field A to Field B for issue ${issue.key}.")
} else {
log.warn("sourceObject is not an ObjectBean. Actual type: ${sourceObject?.getClass()?.name}")
}