Skip to main content
All CollectionsSystem TasksWorking with Groovy ScriptsGroovy Script Library
Groovy Script - Generate Custom XML Output from Form data
Groovy Script - Generate Custom XML Output from Form data
Updated today

Description: Sample groovy script to generate custom XML output from form data.

Click here for detailed help.

Context: App

Remark: Accessible in 'Post' task mode only

Script:

def execute() {

// Get form Details

FormVO formVo = formService.getFormDetails();

String inputXml = formVo.getXmlData();

// Fields to be extracted from Form XML

Map<String, String> dataMap = new HashMap<>();

dataMap.put("ORI_FORMTITLE", formService.getFormFieldValuesFromXML(inputXml, "ORI_FORMTITLE"));

dataMap.put("PROJECT_NAME", formVo.getProjectName());

dataMap.put("FORM_ID", formVo.getFormCodeNum()); // e.g. EMP001, EMP002...

dataMap.put("First_Name", formService.getFormFieldValuesFromXML(inputXml, "First_Name"));

dataMap.put("Last_Name", formService.getFormFieldValuesFromXML(inputXml, "Last_Name"));

dataMap.put("Email_ID", formService.getFormFieldValuesFromXML(inputXml, "Email_ID"));

dataMap.put("Organization", formService.getFormFieldValuesFromXML(inputXml, "Organization"));

dataMap.put("Department", formService.getFormFieldValuesFromXML(inputXml, "Department"));

dataMap.put("Roles", formService.getFormFieldValuesFromXML(inputXml, "Roles"));

dataMap.put("Products", formService.getFormFieldValuesFromXML(inputXml, "Products"));

dataMap.put("AUTHORISER_NAME", "NOT FOUND");

dataMap.put("AUTHORISER_EMAIL", "NOT FOUND");

dataMap.put("TITLE", formService.getFormFieldValuesFromXML(inputXml, "ORI_FORMTITLE"));

//Template to format output XML

String xmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <EMPLOYEE> <EMP_GIVEN_NAME>##ORI_FORMTITLE</EMP_GIVEN_NAME> <PROJECT_NAME>##PROJECT_NAME</PROJECT_NAME> <FORM_ID>##FORM_ID</FORM_ID> <First_Name>##First_Name</First_Name> <Last_Name>##Last_Name</Last_Name> <Email_ID>##Email_ID</Email_ID> <Organization>##Organization</Organization> <Department>##Department</Department> <Roles>##Roles</Roles> <Products>##Products</Products> <AUTHORISER_NAME>##AUTHORISER_NAME</AUTHORISER_NAME> <AUTHORISER_EMAIL>##AUTHORISER_EMAIL</AUTHORISER_EMAIL> <TITLE>##TITLE</TITLE> </EMPLOYEE>";

String outputXml = formService.toXML(dataMap, xmlTemplate);

List<String> toRecipient = new ArrayList<>();

toRecipient.add("email1@example.com");

List<String> cCRecipient = new ArrayList<>();

cCRecipient.add("email2@example.com");

String emailBody = "Hi, <BR/><BR/> New Employee joined ASITE. <BR/><BR/> Thanks, <BR/> Asite Support";

String attachedXmlFileName = formService.getFormFieldValuesFromXML(inputXml, "ORI_FORMTITLE") + "_" + formVo.getFormCodeNum()+".xml";

Map<String, Object> mailDataMap = new HashMap<>();

mailDataMap.put(IGroovyConstant.RECEIPIENT_USERS, toRecipient);

mailDataMap.put(IGroovyConstant.CC_RECEIPIENT_USERS, cCRecipient);

mailDataMap.put(IGroovyConstant.SUBJECT, "Employee Information");

mailDataMap.put(IGroovyConstant.MAIL_BODY, emailBody);

mailDataMap.put(IGroovyConstant.HTML_CONTENT, true);

mailDataMap.put(IGroovyConstant.XML_ATTACHMENT, outputXml);

mailDataMap.put(IGroovyConstant.ATTACHMENT_FILE_NAME, attachedXmlFileName);

notificationService.sendMailWithAttachment(mailDataMap);

}

private String getFileName(String value){

return value.replaceAll("|","_");

}



Did this answer your question?