Skip to Content
Technical Articles

Create custom message for exceptions raised by SAP CPI

There are many times that while we run a big interface there would be a culprit somewhere in the middle who would break the entire interface down, spending time to figure them out would be very expensive!!

Let us consider a scenario a simple scenario where in you have an interface sending data from SAP Successfactors to some 3rd party system. Due to data protection, you wouldn’t want to expose your Employee history or records on message logs. In this case, exception sub process in SAP CPI would capture the error and handle it how ever it is designed to be.

 

Let us say, we have access just to the generic error message where in we don’t have a clue of the employee who is reason behind this error then :

1. You would have to make sure the test run is enabled(if this feature is available, just to be safe the process doesn’t complete while we test ) .

2. Enable the message logging to capture the entire data of the run.

3. Analyse the data to figure the employee.

4. Run the interface just for the single employee and re-check the process.

Executing the above three would be expensive on time considering we are in Production.

As a solution, this is something that i advocate and implement with my customers’ , print the primary key/key element of the message for which the error is been caused.

 

Let us say we have a message which reads or has a content like ” XXXXXXXX employee number : 1234 has failed or has stopped the process to further continue”. With this error message we could land to step 4 directly.

 

Below are the snippets on how we could probably achieve it.

 

iFlow out look:

 

Data in content modifier :

 

Groovy script :

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile

def Message processData(Message message) {
    //Body 
       def body = message.getBody(java.lang.String)as String;
    
        ifgerinv ;
        
        
       return message;
}

 

Data in content modifier of exception process :

 

Groovy script of exception process

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Body 
       def body = message.getBody(java.lang.String)as String;
       def messageHeaders = message.getHeaders();
       
       String EmployeeId               = messageHeaders.get('EmpId');
       String EmployeeDepartment       = messageHeaders.get('EmpDpt');
       String EmployeeHRManagerEmailId = messageHeaders.get('HRMgr');
       String ProcessExceptionMessage  = messageHeaders.get('ExpMessage');
       
       String exceptionBody = 'Employee of id :'+EmployeeId+' ,belonging to the department: '+EmployeeDepartment+' has failed due to :'+ProcessExceptionMessage+' . HR department who monitors the following email would be notified immediately. '+EmployeeHRManagerEmailId ;
    
       throw new Exception(exceptionBody);

       return message;
}

 

Error Message:

 

The only down sight to this would be , the process would be considered as failed and anything after the Groovy script in exception subprocess would be terminated.

I hope this could help you to debug your issues faster and easier.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.