Skip to main content
Generate Custom XML Output from Form data

This article describes the steps to generate custom XML output from form data.

Updated over a month ago

Asite enables generation of custom XML output from existing form data within the project, with the help of workflow system tasks. Below are the steps for configuring workflow to generate XML output:

Step 1: Create a system task with groovy script configured to generate custom XML output from form data.

Below is the snippet of a sample groovy 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()); // 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("|","_");

}

Groovy script configuration includes 3 main stages:


1. Defining information that need to be read

// 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()); // CMI001, CMI002...

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

Script highlighted above is an example where administrator will have to map the information that need to be read from the form.

  • Below is the general syntax for retrieving any information from the form, syntax contains 2 main parameters as explained below:

dataMap.put(“<<Variable Name>>”,<<Function to return Form Field Name>>);

  1. 'Variable Name' could be any name administrator can use to identify the information.

  2. Include function to read the relevant form information.

  • System allows user to read standard/custom form fields defined within the form:

  1. To read standard fields i.e. any field that is captured by default by Asite system. (e.g. Title, Form Originator, Form Created Date, etc...), use the functions to extract standard field as mentioned below:

    1. formVo.getProjectName() – To read project name
      formVo.getFormCodeNum() – To read form code

      Click here to learn more about other standard functions accessible.

      Finally the syntax to read a system field within the groovy script will look like:
      dataMap.put("PROJECT_NAME", formVo.getProjectName()); // This will read project name

    dataMap.put("FORM_ID", formVo.getFormCodeNum()); // This will read form ID e.g. EMP001

  2. To read custom fields i.e. any field placed with the form design other than standard fields( e.g. Employee Name, Employee Email ID, Employee Address, etc...), use the formService function as mentioned below to extract custom field information:

    formService.getFormFieldValuesFromXML(inputXml, "<<Field Name>>"));
    Administrator can replace the ‘Field Name’ with the actual field name from the form design.

    Finally the syntax to read a custom field within the groovy script will look like:
    dataMap.put("First_Name", formService.getFormFieldValuesFromXML(inputXml, "First_Name")); // This will read the data from field named ‘First_Name’

dataMap.put("Last_Name", formService.getFormFieldValuesFromXML(inputXml, "Last_Name")); // This will read the data from field named ‘Last_Name’


2. Defining the output format of the XML

//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);

Script highlighted above is the example how an administrator can define the output format tags in which they expect the XML output. The general syntax to define the output format is:
<’TagName’> ##Variable Name </’TagName>

  • Administrator will have to mention the mapping ‘Variable Name’ from the definitions mentioned in section above. Administrator can add as many XML tags that are required within the final XML output. Finally the syntax for output format of XML within the groovy script will look like:
    <EMPLOYEE> <PROJECT>##PROJECT_NAME</PROJECT> <PROJECTID>##FORM_ID</PROJECTID> <FNAME>##First_Name</FNAME> <LNAME>##Last_Name</LNAME> </EMPLOYEE>


3. Defining email body and recipient of email

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);

}

  • Administrator can configure the groovy script to define the body and recipients of the email.

  • Administrator will have to define the email content in the variable for ‘emailBody’, and then use that variable in the function. Below is an example of defining content within the email body:
    String emailBody = "Hi, <BR/><BR/> A new employee have joined Asite. <BR/><BR/> Thanks";

    1. Administrator can define the recipient of the email, multiple recipient can be added as recipient. Below is an example of defining email recipient:
      List<String> toRecipient = new ArrayList<>();
      toRecipient.add("email1@example.com"); // The mentioned recipient will be main recipient of email
      toRecipient.add("email2@example.com");// The mentioned recipient will be main recipient of email
      List<String> cCRecipient = new ArrayList<>();
      cCRecipient.add("email3@example.com"); // The mentioned recipient will be copy recipient of email

    2. Administrator will then have to use the variable in the relevant functions in the groovy script above, below are the final resultant groovy script function replaced with the variables:

      mailDataMap.put(IGroovyConstant.RECEIPIENT_ADMINISTRATORS, toRecipient);
      mailDataMap.put(IGroovyConstant.CC_RECEIPIENT_ADMINISTRATORS, cCRecipient);
      mailDataMap.put(IGroovyConstant.SUBJECT, "Employee Information"); //This is where administrator will be able to define the email subject
      mailDataMap.put(IGroovyConstant.MAIL_BODY, emailBody);

Step 2: Create a workflow trigger on the required form to use the system task as configured in previous step.

Step 3: Perform the task as per trigger configuration to execute the workflow system task.



Did this answer your question?