MuleSoft

Parse Template & Tabular Format

  • Parse Template is the Mule component to use for processing a template and obtaining a result. A template is defined as text with embedded Mule expressions that are evaluated and replaced with their result.
  • There are some scenarios where we need to use a parse template in the flow. Some of the scenarios are:
    • When we want to prepare the payload to be used by the Web Service Consumer or for a SOAP request.
    • When we want to create a template for emailing.
    • There are other scenarios where it can be useful, such as returning HTML to a client from an HTTP transport. We can load an HTML file into the payload that includes embedded MEL (Mule Expression Language).
  • In this blog, we will see how to use a parse template for creating a template for emailing. The requirement is we need to send data in a tabular format in an attachment. This is commonly used when we need to send an error that occurred in the flow via an email in a structured manner.

Steps:

  1. First, we must create a new Mule Project and drag and drop a listener with HTTP configuration as well as the path mentioned for triggering the flow.

2. After the Listener, use a set variable component for setting the payload. In my case, I am using a payload containing an array that has a JSON object which would contain the data that I need to send in the tabular format.

3. Then we need to have a transform message and this transform message is used to define the content that would be going inside the table and will contain the header for the table which would be in the “th” tag as well as the data that should go inside the table which would be in the “td” tag.

4. Map is used in the case of td tag as there can be multiple data that will be coming inside the array as objects and we need to iterate over the same.

5. Now that we have made the payload into tabular format, we need to use a parse template component to make it into HTML, and for that first, we need to create a template that would be referred to by the Parse Template component.

6. To create the template, we need to create a file inside the src/main/resources folder

7. The extension for the file should be according to the use in my case since I am creating an HTML template it would be .html. For opening the file that is created we should use open with the text editor option.

8. Now we need to create the template for the HTML as below:

9. In the Parse Template component, we need to select the location where we have defined the template so that it can refer the same.

10. We need to use a transform message to create the input for the Email connector which would be in a key-value pair where the key would be the file name for the attachment and the value would be the payload that is coming from the parse template. I am storing it in a variable as I will be making another payload for the body of the email.

11. Now, for creating the body of the email I will be using another parse template and the template that is defined would be as below:

12. You need to refer to the template by using a Parse Template component by pointing to the location where the template is present.

13. After the parse template component, I will be using a transform message component for converting the payload that is coming to a Java format as an input for the email connector.

14. Now we need to configure the email connector, for configuring the connection you can refer to a blog related to email configuration. We need to provide the body and attachment in the email connector and the encoding should be base64.

15. Now we can add a transform message so that we can get a customized message as a response.

16. Now, that the setup is done, the flow will look like this:

17. Now we need to deploy the application and when we hit the application the mail will be shared and you will receive the response. I am using Postman and hitting a request.

18. The email that would be sent would consist of 2 parts the body and the attachment.

The body would be:

When you open the attachment, it will look like:

19. You can customize the table according to your needs and the changes should be made while we are creating the structure for the table.

Author

Vishnu

Leave a comment

Your email address will not be published. Required fields are marked *