Core SAP CPI Library (Must-have)
import com.sap.gateway.ip.core.customdev.util.Message;
This is mandatory in every groovy script. It gives access to:
Header
Property
Body
Java Utility Libraries
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;
We can use these for:
Working with collection
Date Formatting
General Java-style logic
XML Handling (for parsing XML)
import groovy.xml.XmlSlurper;
import groovy.xml.XmlUtil;
import groovy.util.XmlParser;
Use when you want:
Parse XML body
Extract specific nodes/values
Modify or Create XML
JSON Handling
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
Use when your payload or part of it in JSON
Parse JSON in to maps/lists
Convert objects/maps to JSON string
Logging (Optional but Useful)
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.addAttachmentAsString("Payload", message.getBody(String), "text/plain");
To enable logging (in Trace mode only)
Helps in debugging during iFlow development.
Example Script using multiple libraries
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.xml.XmlUtil;
def Message processData(Message message) {
def body = message.getBody(String)
// Example: Parse JSON if body is JSON
def json = new JsonSlurper().parseText(body)
def userId = json.UserID
// Add new header
message.setHeader("UserID", userId)
return message;
}
No comments:
Post a Comment