Add users from a custom asset management/insight field into the request participant field on issue createion

The following script can be used as a post-action on “Create” transition to copy users from a custom assets management field to request participants field (the users will be merged together):

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;

/* Configuration */
log.info("Setting Up Custom Fields...");
def assetsCustomFieldId = 1;
def requestParticipantsCustomFieldId = 2;

/* Custom field with the value to filter on */

CustomField assetsCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(assetsCustomFieldId);        
def assetsValue = issue.getCustomFieldValue(assetsCF);

log.info("Assets Custom Field value is:" + assetsValue);

if (assetsCF == null || assetsValue == null) {
    return true;
}

/* Insight custom field to set */
CustomField requestParticipantsCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(requestParticipantsCustomFieldId);       

log.info("Request Participants Custom Field value is:" + requestParticipantsCF);

if (requestParticipantsCF == null) {
    return true;
}

/* Get Insight IQL Facade from plugin accessor */
log.info("Setting Up IQL Facade");
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade"); 
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);

/* Specify the schema id as well as the IQL that will fetch objects. In this case all objects with Name matching the valueCF, be sure to include " around value */
log.info('Retrieved assets value: ' + assetsValue);
def additionalRequestParticipants = [];
for(assetsObj in assetsValue) {
  def username = assetsObj.label
  def user = ComponentAccessor.getUserManager().getUserByName(username)
  additionalRequestParticipants.add(user)
}
log.info('The following participants will be added: ' + additionalRequestParticipants);

def currentRequestParticipants = issue.getCustomFieldValue(requestParticipantsCF);
log.info('Current participants: ' + currentRequestParticipants);
def newRequestParticipants = currentRequestParticipants + additionalRequestParticipants
log.info('The final list of participants: ' + newRequestParticipants);

MutableIssue mi = (MutableIssue) issue;
mi.setCustomFieldValue(requestParticipantsCF, newRequestParticipants);

return true;

If you have any questions, feel free to reach out! The script is tested and deployed in production.

Updated version of the script to address case when request participants list is empty:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;

/* Configuration */
log.info("Setting Up Custom Fields...");
def boschIdCustomFieldId = 52800;
def requestParticipantsCustomFieldId = 15501;

/* Custom field with the value to filter on */

CustomField BoschIdCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(boschIdCustomFieldId);        
def BoschIDValue = issue.getCustomFieldValue(BoschIdCF);

log.info("Bosch ID Custom Field value is:" + BoschIDValue);

if (BoschIdCF == null || BoschIDValue == null) {
    return true;
}

/* Insight custom field to set */
CustomField requestParticipantsCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(requestParticipantsCustomFieldId);       

log.info("Request Participants Custom Field value is:" + requestParticipantsCF);

if (requestParticipantsCF == null) {
    return true;
}

/* Get Insight IQL Facade from plugin accessor */
log.info("Setting Up IQL Facade");
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade"); 
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);

/* Specify the schema id as well as the IQL that will fetch objects. In this case all objects with Name matching the valueCF, be sure to include " around value */
log.info('Retrieved Bosch IDs: ' + BoschIDValue);
def additionalRequestParticipants = [];
for(boschIdObj in BoschIDValue) {
  def username = boschIdObj.label
  def user = ComponentAccessor.getUserManager().getUserByName(username)
  additionalRequestParticipants.add(user)
}
log.info('The following participants will be added: ' + additionalRequestParticipants);

def currentRequestParticipants = issue.getCustomFieldValue(requestParticipantsCF);
log.info('Current participants: ' + currentRequestParticipants);
def newRequestParticipants = additionalRequestParticipants
if(currentRequestParticipants != null) {
  newRequestParticipants = newRequestParticipants + currentRequestParticipants
}

log.info('The final list of participants: ' + newRequestParticipants);

MutableIssue mi = (MutableIssue) issue;
mi.setCustomFieldValue(requestParticipantsCF, newRequestParticipants);

return true;
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.