Hi,
We have script listener created for create, update and generic events and it is working.
But the problem is the change to the group picker field value is not reflected in the history of the ticket.
Below is the listener script, can anyone kindly help with this?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.user.ApplicationUser
def issue = event.issue as MutableIssue
// CustomFieldManager to access custom fields
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// Get the custom fields
def customField = customFieldManager.getCustomFieldObjectByName("ARC Categories")
def assignedGroup = customFieldManager.getCustomFieldObjectByName("Assigned Group")
def cfVal = issue.getCustomFieldValue(customField) as ArrayList
log.warn "cfValue: $cfVal"
def groupManager = ComponentAccessor.getGroupManager()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (cfVal.size() > 1) {
def firstLevel = cfVal[0] as LazyLoadedOption
log.warn "First level: $firstLevel"
def group
if (firstLevel.getValue() in ["Brokerage", "Asset Management", "Compliance", "Legal", "Super App", "HR/Finance", "External Channels", "Core/Supporting Services", "Institutional Custody", "Digital Supporting Services", "Research"]) {
log.warn "Value present"
// Determine the group to assign based on first-level and second-level values
switch (firstLevel.getValue()) {
case "Brokerage":
group = groupManager.getGroup("ARC-L2-ITO-Brokerage")
break
case "External Channels":
if (cfVal.size() >= 2) {
def secondLevel = cfVal[1] as LazyLoadedOption
if (secondLevel.getValue() == "SuperAPP") {
group = groupManager.getGroup("ARC-L2-ITO-Integration")
} else if (secondLevel.getValue() in ["RCM Web portal"]) {
group = groupManager.getGroup("ARC-L2-ITO-Brokerage")
}
}
break
case "Core/Supporting Services":
group = groupManager.getGroup("ARC UCM_UPM Support")
break
case "Institutional Custody":
group = groupManager.getGroup("ARC-L2-ITO-Internal Systems")
break
case "Digital Supporting Services":
group = groupManager.getGroup("ARC-L2-ITO-Integration")
break
case "Asset Management":
group = groupManager.getGroup("ARC-L2-ITO-Asset Management")
break
case "Compliance":
if (cfVal.size() >= 2) {
def secondLevel = cfVal[1] as LazyLoadedOption
if (secondLevel.getValue() == "KYC") {
group = groupManager.getGroup("ARC-L2-ITO-Internal Systems")
} else if (secondLevel.getValue() in ["Transwatch", "Compliance", "Management System", "Nafith"]) {
group = groupManager.getGroup("ARC-L2-ITO-Internal Systems")
}
}
break
case "Legal":
group = groupManager.getGroup("ARC-L2-ITO-Internal Systems")
break
case "HR/Finance":
group = groupManager.getGroup("ARB")
break
case "Super App":
group = groupManager.getGroup("ARC-L2-ITO-Integration")
if (cfVal.size() >= 2) {
def secondLevel = cfVal[1] as LazyLoadedOption
if (secondLevel.getValue() == "Digital Wallet") {
group = groupManager.getGroup("ARC UCM_UPM Support")
} else {
group = groupManager.getGroup("ARC-L2-ITO-Integration")
}
}
break
}
if (group) {
// Update the Assigned Group field
def changeHolder = new DefaultIssueChangeHolder()
assignedGroup.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(assignedGroup), [group]), changeHolder)
// Log the change in history using ChangeItemBean
def oldValue = issue.getCustomFieldValue(assignedGroup)?.name ?: null
def newValue = group.getName()
def changeItemBean = new ChangeItemBean(ChangeItemBean.CUSTOM_FIELD, assignedGroup.getName(), oldValue, newValue)
changeHistoryManager.addChangeItem(issue, user, changeItemBean)
// Explicitly update the issue to trigger history recording
def issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
// Reindex the issue using IssueIndexingService
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issueIndexingService.reIndex(issue)
log.warn "'Assigned Group' updated to ${group.name} and reflected in history."
}
}
}