This blogs explain about most commonly used groovy scripts when you are working with integration with REST API’s for master data and incremental data delta changes between the source system and target system. We can have a time scheduler in SAP CPI for calling the source system and pull the delta changes records and process to target application.
Delta Change – Incremental Data Replication Between source and target systems.
Business scenario: when we need to call SAP SuccessFactors system with SF ODATA adapter, we can defined first step as a groovy script in SAP CPI iflow and pass the Current Date -1 for yesterday modified or created data synch / or pass the Current date -7 for weekly delta/incremental changes replication between SAP SF and Target REST API application system.

Groovy Script:
***************************************************
import com.sap.gateway.ip.core.customdev.util.Message
def Message processData(Message message) {
def yesterday = new Date() - 1
message.setProperty("yesterday", yesterday.format("yyyy-MM-dd'T'HH:mm:ss.000"))
return message
}
*********************************************************************
//previous day code
//previous day code if you change day -7 it will bring all the recent 7days delta changes for data synch between source and target applications.
Remove Element – Remove the Element Header and Nested Array Tag after the XML to JSON Conversion.
.
When target REST API application designed for accepting the multiple records in array main or nested sub structure we can use the below groovy code for removing the element tag and process the JSON format data in required format.
JSON Nested Array – Structure sample.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
import groovy.json.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def jsonSlurper = new JsonSlurper();
def list = jsonSlurper.parseText(body);
list.customFields = list.customFields.element
list.mandatorySkills=[list.mandatorySkills]
list.scorecard.sections.element.parameters.eachWithIndex {
val,
idx ->
list.scorecard.sections.element.parameters[idx]= list.scorecard.sections.element.parameters.element[idx]
}
//list.scorecard.sections = list.scorecard.sections.element
list.scorecard.sections = [list.scorecard.sections.element]
// the above line array defined with square brackets when you require array structure in target JSON file however this also can be achieved with xml to json coverstion with streaming option - check.
def jsonOP = JsonOutput.toJson(list)
message.setBody(jsonOP);
return message;
}
Removing Element tag after the JSON conversion – when we have single array structure in REST API when we want to remove the element tag from the first line we can use this below groovy.
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String
def jsonParser = new JsonSlurper()
def jsonObject = jsonParser.parseText(body)
message.setBody(JsonOutput.toJson(jsonObject["element"]))
return message;
}
*************
When you are calling the multiple entities from source system and combining with multi mapping with help of below groovy script we can remove the multi mapping tags from required output format.
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;
body = body.replaceAll("<ns0:Messages xmlns:ns0=", "");
// 2ND TAG --
body = body.replaceAll("\"://sap.com/xi/XI/SplitAndMerge\">", ""); // http
body = body.replaceAll("<ns0:Message1>", " ");
// closing 1st tag --
body = body.replaceAll("</ns0:Messages>", "");
body = body.replaceAll("</ns0:Message1>", " ");
//Closing 4th Tag is Root
// body = body.replaceAll("</root>", "");
message.setBody(body);
return message;
}
***************************************************************************
Hope this blog and groovy script helps!!.
Regards,
Krushi Nenavath - Accenture.