I wanted to place an assets attribute values present as an attribute called Groups (used Jira Groups) in a Group Picker field called Assignment Group using a ScriptRunner Post-function based on a value of Asset Object field selected while creating the ticket. I'm using the below script -
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
// Assuming this script is used in a post-function or listener where 'issue' is already available
def issueKey = "ITSC-53" // 'issue' is provided in the context
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject(issueKey)
// actual custom field IDs
def assetFieldId = "customfield_31604" // ID of the asset field
def assignmentGroupFieldId = "customfield_31400" // ID of the Assignment Group field
// Retrieve custom fields by ID
def assetField = customFieldManager.getCustomFieldObject(assetFieldId)
def assignmentGroupField = customFieldManager.getCustomFieldObject(assignmentGroupFieldId)
// Get custom field value from the issue
def assetFieldValue = issue.getCustomFieldValue(assetField)?.toString()
def assetFieldValueString = assetFieldValue.toString()
// Retrieve the list of groups associated with the asset field value
def groupName
switch (assetFieldValueString) {
case "Finance":
groupName = "ITSC: Finance"
break
case "HR":
groupName = "ITSC: HR/HCM"
break
default:
groupName = null
break
}
if (groupName) {
def group = groupManager.getGroup(groupName)
if (group) {
// Set the value in the "Assignment Group" field
issue.setCustomFieldValue(assignmentGroupField, group)
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.debug("Group ${groupName} found for asset value ${assetFieldValue} and set in Assignment Group field.")
} else {
log.warn("Group ${groupName} not found.")
}
} else {
log.warn("No group mapped for the selected asset value.")
}
////////////////////////////////////////////////////////////////////////////////////////////////
I'm getting the WARN - No group mapped for the selected asset value.
I have correct mapping in Assets.
Can anyone help me ASAP? Thanks, in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.