Skip to main content
Groovy Script - Assign Roles to users via Form
Updated this week

Description: Sample groovy script to assign roles to users via form

Context: App

Remark: Accessible in 'Post' task mode only

Script:

def execute(){
// Map the app field to the role names in the format ("form field", "role name")
Map<String, String> assigneeFiledNameAndRoleLabel = new HashMap<String, String>();
assigneeFiledNameAndRoleLabel.put("jbc_contract_manager", "Contract Manager");
assigneeFiledNameAndRoleLabel.put("jbc_project_manager", "Project Manager");
assigneeFiledNameAndRoleLabel.put("jbc_agent", "Agent");
assigneeFiledNameAndRoleLabel.put("jbc_sub_agent", "Sub Agent");
assigneeFiledNameAndRoleLabel.put("jbc_engineer", "Engineer");
assigneeFiledNameAndRoleLabel.put("jbc_quality_rep", "Project Quality Representative");

rolesToUsersAssignment(assigneeFiledNameAndRoleLabel);
}

private FormVO rolesToUsersAssignment(assigneeFiledNameAndRoleLabel){
// Get details of latest form_message created of this app
FormVO formVO = formService.getFormDetails();
// Extract value of specific nodes from form_msg XML
Map<String, List<String>> roleAndUsersMap = formService.getFormFieldValuesFromXML(formVO.getXmlData(), assigneeFiledNameAndRoleLabel);
Map<String, List<Integer>> roleAndUsersIdsMap = new HashMap<String, List<Integer>>();
for(String roleNameXmlField : roleAndUsersMap.keySet()){
List<String> assignee = roleAndUsersMap.get(roleNameXmlField);
List<Integer> assignees = new ArrayList<Integer>();
for(String userDetail : assignee)

{ assignees.add(Integer.parseInt(userDetail(0, 12))); }

roleAndUsersIdsMap.put(assigneeFiledNameAndRoleLabel.get(roleNameXmlField), assignees);
}
// Assign Roles to Users
formService.assignRolesToUsers(roleAndUsersIdsMap, formVO.getCreatorId());

return formVO;

}



Did this answer your question?