Description: Sample Groovy Script to create a new form by fetching details of an existing form.
Context: App
App Type: Select an active app type as required.
Remark: Supported in 'Post' Mode Only.
Script:
def execute(){
/*
*This groovy script will create new Form by fetching details from current Form. Association, Attachments and Distribution is also supported while creating new Form.
*
*/
/************** Configurable parameter section ***************/
String appBuilderCode = "HTML1";
int formMsgTypeId = FormMessageType.ORI_MSG;
String isDistributeByIds = "true";
String associateCurrentFormsAssociatedFiles = "true";
String associateCurrentFormsAssociatedForms = "false";
String copyCurrentFormsAttachment = "true";
String associateCurrentFormToNewForm = "true";
/************** Configurable parameter section ***************/
// Get CURRENT form's xml
boolean getLatestFormMsgDetails = true;
FormVO formVo = formService.getFormDetails(getLatestFormMsgDetails);
String currentFormXml = formVo.getXmlData();
// Add node names to fetch from CURRENT form's xml - mapping for multiple fields can be added
List<String> nodeNameList = new ArrayList<> ();
nodeNameList.add("ORI_FORMTITLE");
// Fetch required nodes from CURRENT form's xml
Map<String, String> nodeValueMap = formService.getFormFieldValuesFromXML(currentFormXml, nodeNameList);
// Target form xml to CURRENT form xml node mapping in same sequence - this is editable
Map<String, String> nodeNameMap = new HashMap<> ();
nodeNameMap.put("ORI_FORMTITLE", "ORI_FORMTITLE");
// Get XML template of NEW Form message to be created - no changes required to be made here
FormTypeVO formTypeVO = formService.getFormTypeDetail(appBuilderCode);
String newFormXmlTemplate = formTypeVO.xmlData;
// Prepare NEW form xml - no changes required to be made here
String newFormXml = formService.updateXMLNodeByNodeName(newFormXmlTemplate, nodeNameMap, nodeValueMap); // Parameters: 1. NEW Form xml template string 2. NEW to CURRENT Form xml node mapping MAP 3. CURRENT form's nodes values
// Prepare Distribution for NEW form
// Fetch recipient details from current Form - this section is editable
List<String> currentFormFieldList = new ArrayList<> ();
currentFormFieldList.add("SelectedUser");
currentFormFieldList.add("Dropdown_4");
currentFormFieldList.add("Datepicker_1");
Map<String, List<String>> currentFormFieldValueMap = formService.getFormFieldValuesForRepeatingTable(currentFormXml, currentFormFieldList);
List<DistributionVO> distVoList = new ArrayList<DistributionVO>();
for(int i=1; i<currentFormFieldValueMap.get("SelectedUser").size(); i++) {
int userId = Integer.parseInt(currentFormFieldValueMap.get("SelectedUser").get(i).split("#")[0]); // 123456#Andy Architect, ABC Solutions
int actionId = Integer.parseInt(currentFormFieldValueMap.get("Dropdown_4").get(i).split("#")[0]); // 7#For Information
int dueDays = formService.getActionDaysFromDate(currentFormFieldValueMap.get("Datepicker_1").get(i), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", formVo.getCreatorId()); // 2023-02-25T09:07:41.000Z
prepareDistribution(Recipient.USER, userId, actionId, dueDays, distVoList);
}
// Prepare request map to create NEW form
Map<String, Object> createFormMap = new HashMap<> ();
createFormMap.put(ISystemConstantsDM.APP_BUILDER_CODE, appBuilderCode);
createFormMap.put(ISystemConstantsDM.FORM_MSG_TYPE, formMsgTypeId);
createFormMap.put(ISystemConstantsDM.FORM_DIST_LIST, distVoList);
createFormMap.put(ISystemConstantsDM.IS_DIST_FOR_IDS, isDistributeByIds); // Distribution is supported by IDs and Name both, true means by IDs and false means by Names. By default this will be false, no need to set this parameter to set false.
createFormMap.put(ISystemConstantsDM.ASSOCIATE_CURRENT_FORM_ASSOCIATED_FILES, associateCurrentFormsAssociatedFiles); // by default this will be false, no need to set this parameter to set false.
createFormMap.put(ISystemConstantsDM.ASSOCIATE_CURRENT_FORM_ASSOCIATED_FORMS, associateCurrentFormsAssociatedForms); // by default this will be false, no need to set this parameter to set false.
createFormMap.put(ISystemConstantsDM.COPY_CURRENT_FORM_ATTACHMENTS, copyCurrentFormsAttachment); // by default this will be false, no need to set this parameter to set false.
createFormMap.put(ISystemConstantsDM.ASSOCIATE_CURRENT_FORM_TO_NEW_FORM, associateCurrentFormToNewForm); // by default this will be false, no need to set this parameter to set false.
createFormMap.put(ISystemConstantsDM.XML_DATA, newFormXml);
// Create NEW FORM - no changes required to be made in this section
FormVO createdFormVO = formService.createForm(createFormMap);
}
def prepareDistribution(def recipientType, def recipientId, def actionName, def actionDueDays, def distVoList){
DistributionVO vo = new DistributionVO();
vo.setRecipientType(recipientType);
//vo.setRecipientName(recipientName);
vo.setRecipientId(recipientId);
vo.setActionId(actionName);
vo.setDueDays(actionDueDays);
vo.setSendMail(false);
distVoList.add(vo);
}