Skip to main content
All CollectionsSystem TasksWorking with Groovy ScriptsGroovy Script Library
Groovy Script - Dynamic Distribution of Forms based on User's Email, Role or Distribution Group
Groovy Script - Dynamic Distribution of Forms based on User's Email, Role or Distribution Group
Updated today

Description: Sample groovy script for dynamic distribution of forms based on name of user, role or distribution group.

Context: App

Remark: Accessible in 'Post' task mode only

Script:

/**

* Executes the preparation of a dynamic distribution list to be used in the User Task node of a Visual Workflow.

*/

def execute() {

/*

* This script will prepare a dynamic distribution list to be used in the User Task node of Visual Workflow.

* The distribution list is based on the the form details and the allowed actions and recipient types.

*/

// Define constants for allowed actions for the forms

// Note: The values shown here are examples, update them based on actual use case.

/*

* SystemActionsForms.ACTION_ASSIGN_STATUS

* SystemActionsForms.ACTION_FORM_RESPOND

* SystemActionsForms.ACTION_DISTRIBUTE

*/

// Define constants for allowed recipient data types

// Note: The values shown here are examples, update them based on actual use case.

/*

* IGroovyConstant.DISTRIBUTE_BY_ID

* IGroovyConstant.DISTRIBUTE_BY_NAME

*/

// Get form Details

FormVO formVo = formService.getFormDetails();

//String status = formVo.getFormStatusName();

String inputXml = formVo.getXmlData();

// Fields to be extracted from Form XML

String outputXmlValue = formService.getFormFieldValuesFromXML(inputXml, "ORI_FORMTITLE");

// Initialize the map to hold user task distributions

Map<String, DynamicDistributionVO> userTaskDistributionMap = new HashMap<String, DynamicDistributionVO>();

if(outputXmlValue.equalsIgnoreCase("Open")){

List<DistributionVO> receipientsList = new ArrayList<DistributionVO>();

// Add user emails

def user = new HashSet<>();

user.add("user1@example.com");

user.add("user2@example.com");

prepareDistributionList(Recipient.USER, user, receipientsList);

// Add roles

def roles = new HashSet<>();

roles.add("Example Role Name");

prepareDistributionList(Recipient.ROLE, roles, receipientsList);

// Add groups

def group = new HashSet<>();

group.add("Example Distribution Group Name");

prepareDistributionList(Recipient.GROUP, group, receipientsList);

// Add the dynamic distribution for User Task 1 to the map

/**

* @param userTaskName The name of the user task.

* @param actionName The identifier for the action associated with the user task.

* @param dueDays The number of days allowed for completing the user task.

* @param receipientsList A list of DistributionVO objects containing recipients and their types.

* @param userTaskDistributionMap The map containing user task names as keys and corresponding dynamic distribution as values.

*/

// if 'Respond' is action selected then views should be mandatory

addUserTaskDistribution("User Task 1", SystemActionsForms.ACTION_FORM_RESPOND, 1 , "RES_VIEW", receipientsList, userTaskDistributionMap);

}else if(outputXmlValue.equalsIgnoreCase("Approved")){

List<DistributionVO> receipientsList = new ArrayList<DistributionVO>();

// Add user emails

def user = new HashSet<>();

user.add("user3@example.com");

user.add("user4@example.com");

user.add("user5@example.com");

prepareDistributionList(Recipient.USER, user, receipientsList);

// Add roles

def roles = new HashSet<>();

roles.add("Example Role Name 1");

roles.add("Example Role Name 2");

prepareDistributionList(Recipient.ROLE, roles, receipientsList);

// Add groups

def group = new HashSet<>();

group.add("Example Form Distribution Group 1 Name");

group.add("Example Form Distribution Group 2 Name");

prepareDistributionList(Recipient.GROUP, group, receipientsList);

// Add the dynamic distribution for User Task 2 to the map

/**

* @param userTaskName The name of the user task.

* @param actionName The identifier for the action associated with the user task.

* @param dueDays The number of days allowed for completing the user task.

* @param receipientsList A list of DistributionVO objects containing recipients and their types.

* @param userTaskDistributionMap The map containing user task names as keys and corresponding dynamic distribution as values.

*/

addUserTaskDistribution("User Task 2", SystemActionsForms.ACTION_ASSIGN_STATUS , 2, "", receipientsList, userTaskDistributionMap);

}else{

// If the document status is neither "Open" nor "Approved", prepare <DEFAULT> distribution

List<DistributionVO> receipientsList = new ArrayList<DistributionVO>();

// Add user emails

def user = new HashSet<>();

user.add("user6@example.com");

prepareDistributionList(Recipient.USER, user, receipientsList);

// Add roles

def roles = new HashSet<>();

roles.add("Example Role Name");

prepareDistributionList(Recipient.ROLE, roles, receipientsList);

// Add groups

def group = new HashSet<>();

group.add("Example Form Distribution Group Name");

prepareDistributionList(Recipient.GROUP, group, receipientsList);

// Add the dynamic distribution for <DEFAULT> with an empty task name

/**

* @param userTaskName The name of the user task.

* @param actionName The identifier for the action associated with the user task.

* @param dueDays The number of days allowed for completing the user task.

* @param receipientsList A list of DistributionVO objects containing recipients and their types.

* @param userTaskDistributionMap The map containing user task names as keys and corresponding dynamic distribution as values.

*/

addUserTaskDistribution("", SystemActionsForms.ACTION_DISTRIBUTE , 3, "", receipientsList, userTaskDistributionMap);

}

formService.setDynamicDistribution(userTaskDistributionMap,IGroovyConstant.DISTRIBUTE_BY_NAME);

}

/**

* Adds a user task distribution configuration to the given map of user task distributions.

* This function creates a dynamic distribution for a specific user task and associates it with the provided parameters.

* The dynamic distribution contains details about the task, action, due days, and the list of recipients.

*

* @param userTaskName The name of the user task.

* @param actionName The identifier for the action associated with the user task.

* @param dueDays The number of days allowed for completing the user task.

* @param formView The form view to be assined to user task for the respond action

* @param distVoList A list of DistributionVO objects containing recipients and their types.

* @param userTaskDistribution The map containing user task names as keys and corresponding dynamic distribution as values.

*/

def addUserTaskDistribution(String userTaskName, Integer actionName, Integer dueDays, String formView, List<DistributionVO> distVoList, Map<String, DynamicDistributionVO> userTaskDistribution) {

// Create a new dynamic distribution object

DynamicDistributionVO dynamicDistributionVO = new DynamicDistributionVO();

// Set the details of the dynamic distribution

dynamicDistributionVO.setUserTaskName(userTaskName);

dynamicDistributionVO.setActionName(actionName);

dynamicDistributionVO.setDueDays(dueDays);

dynamicDistributionVO.setDistributionList(distVoList);

dynamicDistributionVO.setFormView(formView);

// Add the dynamic distribution to the userTaskDistribution map with the user task name as the key

userTaskDistribution.put(userTaskName, dynamicDistributionVO);

}

/**

* Prepares a distribution list for a given recipient type (GROUP, ROLE, USER).

* This function creates a DistributionVO object with the recipient type and recipient name list,

* and adds it to the provided distribution list.

*

* @param recipientType The type of recipient (GROUP, ROLE, USER).

* @param receipientsList The names of the recipients based on the recipientType.

* @param distVoList The list where the DistributionVO object will be added.

*/

// For GROUP, ROLE, USER.

def prepareDistributionList(def recipientType, def receipientsList, def distVoList){

// Create a new DistributionVO object

DistributionVO vo = new DistributionVO();

// Set the recipient type and recipient name for the distribution

vo.setRecipientType(recipientType);

vo.setRecipientList(receipientsList);

// Add the DistributionVO object to the distribution list

distVoList.add(vo);

}



Did this answer your question?