Incorporating email alerts within SAP Cloud Integration is a critical functionality that significantly enhances the efficiency of our operations team. Therefore, it is imperative for the development team to ensure the integration of email alert mechanisms in all their integration flows (iflows). Furthermore, they should prioritize presenting pertinent information in a concise, user-friendly, and aesthetically pleasing format within these email alerts.
In this blog post, I will be sharing a Groovy script that covers the creation of the email body for alert messages.
The primary objective is to consolidate all essential information within the email, transforming it into a comprehensive resource. This approach enables team members to access relevant details directly from the email, eliminating the need to navigate to the monitoring page to check for that particular message.
This blog addresses email notifications for both success and failure reporting. To make success email notifications optional, you can simply include a property in a content modifier and configure the router to bypass the email notification block when necessary. For failure notifications, as this block is integrated into all sub-exception processes throughout the integration flow, it guarantees the generation of emails for various types of failures, providing comprehensive error exception details consistently.
Looking at the code, call all the relevant dynamic properties which are important in terms of reporting, there are few by default properties and environmental variables provided by SAP which can be used in the email alert as per our need.
Below table shows the properties which the developer has created.
Property Name |
Significance |
IflowName |
Name of Iflow |
packageName |
Name of the Integration Package |
Lip_name |
LIP name where the failure has occurred |
ArchivePath |
Archive Directory |
fileName |
Archival Filename |
ReceiverFileName |
Receiver Filename |
ReceiverPath |
Receiver Directory |
TO_ErrorRecords |
“TO” Recipient of Error emails |
CC_ErrorRecords |
“CC” Recipient of Error emails |
TO_SuccessEmail |
“TO” Recipient of Success emails |
CC_SuccessEmail |
“CC” Recipient of Success emails |
Below table shows the properties and variables provided by SAP
Property/Variable Name |
Significance |
SAP_MessageProcessingLogID |
Property which stores the message ID of the message |
System.getenv |
The variable which stores the tenant details |
CamelExceptionCaught |
Property which stores error exception details |
exceptionName |
Stores exception name |
exceptionMessage |
Stores exception message |
Groovy Script :
- System.env is used to get the details of the tenant to understand the alert is coming from which tenant either QA or Production tenant
- Two Blocks (if else) : Error block and Success Block, the body is generated based on these if and else statements
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.IOException;
import java.util.Arrays;
import javax.mail.util.ByteArrayDataSource
import org.apache.camel.Exchange
import org.apache.camel.builder.SimpleBuilder
def Message processData(Message message)
{
def map = message.getProperties();
def headers = message.getHeaders();
def iflowName = map.get("IflowName");
def msgID = map.get("SAP_MessageProcessingLogID");
def packageName = map.get("packageName");
def body = message.getBody(java.lang.String) as String;
def mailBody = "";
def lip_name = map.get("Lip_Name");
def today = new Date();
def todayDate = today.format("dd-MM-yyyy",TimeZone.getTimeZone('IST'))
def todayTime = today.format("HH:mm:ss",TimeZone.getTimeZone('IST'))
def ArchivePath = map.get("ArchivePath")
def subject = "";
def fileName = map.get("ArchiveFileName");
def mailTo = ""
def mailCC = ""
def TenantID = "";
def ReceiverFileName = map.get("ReceiverFileName")
def ReceiverPath = map.get("ReceiverPath")
String systemName = System.getenv("TENANT_NAME")
String url = System.getenv("TENANT_NAME")+"."+System.getenv("IT_SYSTEM_ID")+"."+System.getenv("IT_TENANT_UX_DOMAIN")
def msgIDLink = "https://"+url+":443/itspaces/shell/monitoring/MessageDetails/%7B%22messageGuid%22%3A%22"+msgID+"%22%7D";
if(systemName.equals("e****"))
{
TenantID = "FieldCore SAP CPIS Test tenant"
}
else
{
TenantID = "FieldCore SAP CPIS Production tenant"
}
def ex = map.get("CamelExceptionCaught");
if (ex!=null)
{
subject = "Error - "+iflowName;
subject = subject.replaceAll("Packaged Integration -","");
def exceptionName ="";
def exceptionMessage = "";
def link = "";
mailTo = map.get("TO_ErrorRecords");
mailCC = map.get("CC_ErrorRecords");
exceptionName = ex.getClass().getName();
exceptionMessage = ex.getMessage();
mailBody = "<html><head><style>table, th, td {"+
"border: 1px solid black;"+
"}</style></head><body>"+
"<table><tr><th colspan='2'><b><center>"+iflowName+"</center></b></th></tr>"+
"<tr><td><b>CPI System ID</b></td><td>"+TenantID+"</td></tr>"+
"<tr><td><b>Package Details</b></td><td>"+packageName+"</td></tr>"+
"<tr><td><b>Date of Run </b></td><td>"+todayDate+" <b>IST</b> </td></tr>"+
"<tr><td><b>Time of Run </b></td><td>"+todayTime+" <b>IST</b></td></tr>"+
"<tr><td><b>LIP name</b></td><td>"+lip_name+"</td></tr>"+
"<tr><td><b>Archive File Name</b></td><td>"+fileName+"</td></tr>"+
"<tr><td><b>SFTP Archive File Path</b></td><td>"+ArchivePath+"</td></tr>"+
"<tr><td><b>Schwab File Name</b></td><td>"+ReceiverFileName+"</td></tr>"+
"<tr><td><b>Schwab Directory</b></td><td>"+ReceiverPath+"</td></tr>"+
"<tr><td><b>Message ID </b></td><td><a href='"+msgIDLink+"'>"+msgID+"</a></td></tr>"+
"<tr><th colspan='2'><b><center>Exception Details</center></b></th></tr>"+
"<tr><td><b> Name of Exception </b></td><td>"+exceptionName+"</td></tr>"+
"<tr><td><b> Details of Excepton </b></td><td>"+exceptionMessage+"</td></tr></table>"+
"<br/><p>Thanks and Regards<br/>Integration Team</p>"+
"<p>** This is an auto generated email **</p>"+
"</body></html>";
}
else
{
mailTo= map.get("TO_SuccessEmail");
mailCC = map.get("CC_SuccessEmail");
subject = "Success - "+iflowName
subject = subject.replaceAll("Packaged Integration -","");
mailBody = "<html><head><style>table, th, td {"+
"border: 1px solid black;"+
"}</style></head><body>"+
"<table><tr><th colspan='2'><b><center>"+iflowName+"</center></b></th></tr>"+
"<tr><td><b>CPI System ID</b></td><td>"+TenantID+"</td></tr>"+
"<tr><td><b>Package Details</b></td><td>"+packageName+"</td></tr>"+
"<tr><td><b>Date of Run </b></td><td>"+todayDate+" <b>IST</b></td></tr>"+
"<tr><td><b>Time of Run </b></td><td>"+todayTime+" <b>IST</b></td></tr>"+
"<tr><td><b>Archive File Name</b></td><td>"+fileName+"</td></tr>"+
"<tr><td><b>SFTP Archive File Path</b></td><td>"+ArchivePath+"</td></tr>"+
"<tr><td><b>Schwab File Name</b></td><td>"+ReceiverFileName+"</td></tr>"+
"<tr><td><b>Schwab Directory</b></td><td>"+ReceiverPath+"</td></tr>"+
"<tr><td><b>Message ID </b></td><td><a href='"+msgIDLink+"'>"+msgID+"</a></td></tr></table>"+
"<br/><p>Thanks and Regards<br/>Integration Team</p>"+
"<p>** This is an auto generated email **</p>"+
"</body></html>";
}
message.setProperty("TO", mailTo);
message.setProperty("CC", mailCC);
message.setProperty("body", mailBody);
message.setProperty("mailSubject", subject);
return message;
}
Email Adapter Setting: As the body of the email is of HTML type, the body Mime-Type has to be set as "Text/HTML" in the Mail Adapter.
Mail Adapter Configuration
Success Email will look like as shown in the below image, Please note "Success" keyword is maintained in the subject line
Success Email
Error Email will look like as shown in the below image, Please note "Error" keyword is maintained in the subject line for immediate attention
Error Email
The purpose of this blog is to demonstrate the effective formatting and presentation of email alerts. You have the flexibility to customize the properties to align with your specific requirements and integration scenarios. I chose these properties based on their relevance to my integration flow.
I hope you have gained an understanding of the core concept of enhancing email notifications through this blog.
Cheers,
P