Skip to main content
All CollectionsSystem TasksWorking with Groovy ScriptsGroovy Script Library
Groovy Script - Dynamic Distribution of Files based on User's Email, Role or Distribution Group
Groovy Script - Dynamic Distribution of Files based on User's Email, Role or Distribution Group
Updated over a week ago

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

To save an empty due date for the ‘FOR_INFORMATION’ task, you can set the due date as ‘-1’.

Context: Document

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 status of the document and the allowed actions and recipient types.

*/

// Define constants for allowed actions for the document

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

/*

* SystemAction.FOR_ACKNOWLEDGEMENT

* SystemAction.FOR_COMMENT

* SystemAction.FOR_COMMENT_COORIDINATION

* SystemAction.FOR_COMMENTS_INCORP

* SystemAction.FOR_DISTRIBUTE

* SystemAction.FOR_INFORMATION

* SystemAction.FOR_STATUS_CHANGE

* SystemAction.FOR_ACTION

*/

// 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 document details

DocumentVO documentVO = documentService.getDocumentDetails();

// Initialize the map to hold user task distributions

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

if(documentVO.getStatus().equalsIgnoreCase("Open")){

// If the document status is "Open", prepare distribution for User Task 1

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 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.

*/

addUserTaskDistribution("User Task 1", SystemAction.FOR_ACKNOWLEDGEMENT , 1, receipientsList, userTaskDistributionMap);

}else if(documentVO.getStatus().equalsIgnoreCase("Approved")){

// If the document status is "Approved", prepare distribution for User Task 2

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("GP1");

group.add("Example File Distribution Group 1");

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 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.

*/

addUserTaskDistribution("User Task 2", SystemAction.FOR_COMMENT_COORIDINATION , 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 1");

prepareDistributionList(Recipient.ROLE, roles, receipientsList);

// Add groups

def group = new HashSet<>();

group.add("Example File Distribution Group 1");

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 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.

*/

addUserTaskDistribution("", SystemAction.FOR_STATUS_CHANGE , 3, receipientsList, userTaskDistributionMap);

}

documentService.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 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, 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);

// 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?