Skip to Content
Technical Articles

SAP CPI – How to create dynamic HTML Email body using groovy script.

Introduction: –

In the Integration world there is a very common requirement of the clients to send an email notification to different people and content of the email should be dynamic. Here I have tried to explain how we can achieve this requirement in SAP CPI.

I have taken an example of a SuccessFactors Integration scenario where we need to send dynamic HTML email content based on the data in SuccessFactors. You can use the below concept in any other scenario where you have a similar kind of a requirement.

Problem statement & Resolution:-

Requirement is to send an email notification to all the employees of an organization every month with the details of all the incomplete trainings they have at that point of time. Details of all the incomplete trainings should be formatted into a table and sent in an email.

Resolution: – I have showed this solution for one employee for the ease of presentation. But similar approach can be used for all employees to send bulk email notification.

First, I need to collate all the training details in an xml structure. Consider the below xml structure which I got for an employee after collating all the data. Steps before this structure is not explained in this blog as this can be easily achieved by using request reply and message mapping steps.


Structure: –

    <TrainingName>Data Security</TrainingName>
    <TrainingName>Ethics and Compliance</TrainingName>


Now I have used a message mapping step to convert this structure into the below one. The idea is to filter the training records whose Due date is less that current date (10th Aug 2020) and get all the training data which must be sent in an email into one field of a xml structure. So as you can see second training record has been filtered out because due date of that training is greater than current date.


    <MailData>Data Security, 2020-05-01, 2020-09-01|Onboarding, 2020-05-01, 2020-08-01</MailData>


I have used a groovy script inside graphical mapping step. Context of all the fields in the mapping is set to ‘root’. Then I have used the below groovy script to achieve this result.

Groovy script used:-

def void MailData (String [] TrainingName,String [] AssignedDate, String [] DueDate, String[] CurrDate, Output output, MappingContext context){
    String pattern = "yyyy-MM-dd"
    def Curr_Date = new SimpleDateFormat(pattern).parse(CurrDate[0]);
    String Ans = "";
    for (i=0;i<DueDate.size();i++){
        def Due_Date = new SimpleDateFormat(pattern).parse(DueDate[i]);
        if (Due_Date < Curr_Date){
            Ans = Ans + TrainingName[i] + "," + AssignedDate[i] + "," + DueDate[i] + "|"


Then I have stored this MailData into a property by using content modifier.



Now by using this property I have formatted the HTML Email table content by using the below mentioned groovy script.

def Message FormatEmailData(Message message) 
    def map = message.getProperties();
    def MailData = map.get("MailData");
    int count = 0;
    String breaks = "";

    String MailDATA = '<table width="80%" border="1" align="left" border-collapse="collapse"><tr><th align="left" bgcolor="#add8e6">Training Name</th><th align="left" bgcolor="#add8e6" >Assigned Date</th><th align="left" bgcolor="#add8e6">Due Date</th></tr>'
	    if (MailData.charAt(i) == "|"){
	for (j=1;j<=count;j++){
	    String main = MailData.substring(0,MailData.indexOf('|'));
	    String[] Main = main.split(',');
	    String TrainingName = Main[0];
	    String AssignedDate = Main[1];
	    String DueDate = Main[2];
	    MailDATA = MailDATA + "<tr><td>" + TrainingName + "</td><td>" + AssignedDate + "</td><td>" + DueDate + "</td></tr>"
	    if (j != count){
	    MailData = MailData.substring(MailData.indexOf('|') + 1);
	    breaks = breaks + '<br></br>';

	MailDATA = MailDATA + "</table>" + breaks;
    return message;



This script will store the HTML formatted data into a the property named ‘MailData’ . After the basic configuration of mail adapter, select Body Mime-Type as ‘Text/HTML”. In the Email body I have used the above mentioned property which will represent the data in systematic tabular form.


Now when we run the interface the employee will get an email as shown below.


Conclusion: –

This is how you can achieve dynamic HTML email content using message mapping and groovy scripts. You can use the same concept for your requirements and can play around with the HTML tags  in the groovy script to get the desired mail content.

Hope this blog post helps 🙂



Dhairya Khimsaria

You must be Logged on to comment or reply to a post.