[SAP CPI] – BASIC EXAMPLE GROOVY SCRIPT

Hi guys, in this article I want to share some groovy script. Hope these script will useful in your integration flow. Here we go.

  • Write log to capture all payload
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message)
{   
    def body = message.getBody(java.lang.String) as String;
    def messageLog = messageLogFactory.getMessageLog(message);
    if(messageLog != null)
    {
        messageLog.setStringProperty("log1","Printing Payload As Attachment")
        messageLog.addAttachmentAsString("log1",body,"text/plain");
    }
    return message;
}
  • Write log incoming payload only if there is as exception
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
       def map  = message.getProperties();
       def ex   = map.get("CamelExceptionCaught");
       if (ex != null)
       {
           exceptionText    = ex.getMessage();
           def messageLog   = messageLogFactory.getMessageLog(message);
           messageLog.addAttachmentAsString("Exception", exceptionText,"application/text");
       }
       return message;
}
  • Make your integration flow stop, pause, or sleep for sometime
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
       def body = message.getBody();
       sleep(40000);
       message.setBody(body);
       return message;
}
  • Remove something from incoming payload. Ex : “<?xml version=”1.0” endcoding = “UTF-8″?>”
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
    def removal=message.getBody(java.lang.String) as String;
    removal=removal.replace(/<?xml version="1.0" encoding="UTF-8"?>/,"");
    message.setBody(removal);
    return message;
}
  • Get all properties and header
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.*;
def Message processData(Message message)
{
    map = message.getProperties();
    return message;
}
  • Get all properties ( Property, Header), edit in script and use output as ${header.newHeader} or ${property.newProperty}
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    map = message.getProperties();
    def zID = map.get("ID");
    def indicator;
    if(zID == '')
    {
        indicator = 'false';
    }
    else
    {
        indicator = 'true';
    }
    message.setProperty("Result", indicator);
    return message;
}
  • Concatenate all properties
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.*;
def Message processData(Message message)
{
    map = message.getProperties();
    def ZF1    = map.get("F1");
    def ZF2    = map.get("F2");
    def ZF3    = map.get("F3");
    String ZConcat = ZF1.concat(ZF2).concat(ZF3);
    message.setProperty("ConcatenatedResult",ZConcat);
    return message;
}
  • Add days to a date
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.*;
import com.sap.it.api.mapping.*;
def Message processData(Message message)
{
    Date zcurDate = new Date();
    Date zNewDate = zcurDate + 30;
    message.setProperty("Current_Date",zcurDate);
    message.setProperty("New_Date",zNewDate);
    return message;
}

Or

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.*;
import com.sap.it.api.mapping.*;
def Message processData(Message message)
{
    def zNewDate = new Date().plus(30)
    message.setProperty("New_Date",zNewDate);
    return message;
}
  • Write function which use in message mapping
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.mapping.*;
def String datePlus30(String header,MappingContext context){
    Date zcurDate = new Date();
    Date znewDate = zcurDate + 30;
    return znewDate.format("yyyyMMdd");
}
  • Function return null in message mapping
import com.sap.it.api.mapping.*;
def String customFunc(String arg1){
	return null
}
  • Return Message Processing Log ID ( MPL ID) and use it in Message Mapping
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.mapping.*;

def String getMessageProcessingLogID(String header,MappingContext context)
{
        String mplId = context.getHeader("SAP_MessageProcessingLogID").toString();
        return mplId;
}

Summary

Above is all basic groovy script useful for SAP CPI. Some case, if you want to take real result you can go to link : https://groovyide.com/cpi to check result after run your groovy script. Thanks for your reading and If have any question, kindly leave your comment in below this article.

Thanks.

Joseph.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.