Pick only integer value from alphanumeric string in message mapping
import com.sap.it.api.mapping.*;
def String extractNumeric(String arg1){
def str = arg1.replaceAll("\\D+","");
return str
}
This blog is created to throw some lights on SAP CPI concepts, which experienced in my journey. Trying to explore more about HCI/Cloud platform integration/SCPI and sharing the contents to help you. Thanks for all Users your support. Please give your comments below ...
Pick only integer value from alphanumeric string in message mapping
import com.sap.it.api.mapping.*;
def String extractNumeric(String arg1){
def str = arg1.replaceAll("\\D+","");
return str
}
To remove the duplicate lines in CSV file:
Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.util.*;
//import java.io.file.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def myList = body.tokenize("\n")*.trim()
myList = myList.unique()
body = myList
// message.setBody(body)
message.setProperty("BusinessPartner", myList);
return message;
}
Remove dummy fields from Json:
Groovy script:
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)
def rem = []
for(i=0;i<=20;i++){
def toseries = "dummy${i}"
rem << toseries
}
println rem.size()
def toRemove = rem
//def toRemove =["dummy0","dummy1"]
println toRemove
toRemove.each { field ->
jsonObject.remove(field.toString())
message.setBody(JsonOutput.toJson(jsonObject))}
return message;
}
Find all the messages from the unformatted xml and place in newline:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.util.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def xml = new XmlSlurper().parseText(body)
def error = xml.'**'.findAll { it.name() == 'message' }*.text().join('\n')
message.setBody(error);
return message;
}
CSV file to be prepared with multiple header with multiple line item on it
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.xml.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def ag = new xml2csv()
message.setBody(ag.execute(body))
return message
}
class xml2csv {
def execute(ins) {
//define writer
def writer = new StringWriter()
//parse input
def parsedXml = new XmlParser().parseText(ins)
def content = new XmlSlurper().parseText(ins)
def header = content.Header.children().collect().join(',')
def csv = content.Item.inject(header){ result, row ->
[result, row.children().collect().join(',')].join("\n")
}
println csv.toString()
return csv.toString()
}
}
Requirementis to send the report on particular date's on the month in groovy script.
Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper
import groovy.json.JsonException
import groovy.json.JsonOutput
import javax.xml.*;
import groovy.xml.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def date = new Date()
def day = date.getDate()
def Flag = ""
if(day == 20 || day == 21 || day == 22 || day == 23){
Flag = "true"
}
else {
Flag = "false"
}
message.setProperty("Flag",Flag)
return message;
}
Get all the day from the input 3 fields and check if it matching with today day then return true or false.
Day comparison script
Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper
import groovy.json.JsonException
import groovy.json.JsonOutput
import javax.xml.*;
import groovy.xml.*
def Message processData(Message message)
{def propertyMap = message.getProperties()
String date2 = propertyMap.get("ExectutionDate");
String date3 = propertyMap.get("ExectutionDate1");
String date4 = propertyMap.get("ExectutionDate2");
def date1 = new Date()
String day = date1.getDate()
if (day == date2 || day == date3 || day == date4 )
{ Flag = "true"; }
else {Flag = "false" }
message.setProperty("Flag1", Flag)
message.setProperty("day1", day)
return message;}
URL Encode the query:
Data: "ADDRESS": "+GleuelerStr.70\u00a0Building5,floor2A,room015+Cologne\u00a0NorthRhine-Westphalia+50931",
This char "\u00a0" is causing the issue, so can't pull location info from GoogleAPI.
Google script line:
inputurl = URLEncoder.encode(inputurl, "UTF-8")
We can get the response.
Set timezone in Content modifier directly without script:
${date-with-timezone:now:America/Los_Angeles:yyyy-MM-dd'T'HH:mm:
Remove the new line for the XML to CSV conversion:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
def content= message.getBody(java.lang.String); // Read Body
newcontent=content.replaceAll("[\n\r]", "")
message.setBody(newcontent); // Set message body
return message;
}
To check if the first character is number or string:
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);
def firstChar = body.substring(0..1)
if (firstChar.matches("[0-9]+")){
number= "true"
}else
{
number = "false"
}
message.setProperty("number",number)
return message;
}
Groovy script:
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);
//message.setBody(body);
def num = body.replaceAll("[^0-9]", "")
message.setProperty("num",num)
return message;
}
Input: 43645fdggtshf
Output: num: 43645
Groovy script:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message)
{
def content= message.getBody(java.lang.String); // Read Body
def newcontent=content.replaceAll('\n\n', '\n');
message.setBody(newcontent); // Set message body
return message;
}
XSLT Mapping:
<xsl:template match="//ns1:SupplierInformation">
<xsl:copy-of select="."/>
</xsl:template>
Convert XML to CSV conversion different option:
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.util.*
import groovy.xml.XmlUtil;
import groovy.util.XmlSlurper;
import groovy.xml.MarkupBuilder;
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def det
def body1
def lines = body.split("\n")
def CAMPAIGN = lines[1].trim()
def POLY = lines[3].trim()
def BATCH = lines[5].trim()
def UOM = lines[7].trim()
def PRODUCT = lines[9].trim()
def LINE = lines[11].trim()
def VERSION = lines[13].trim()
def TIMESTAMP = lines[15].trim()
def ABORTED = lines[17].trim()
for(i=19;i<=lines.size();i++)
{
if(lines[i-1].trim() != "EVENTS:"){
det = i;
}
else{ break;}
}
for(j=19;j<det;j++)
{
def data = lines[j].trim()
data = data.split(/\s+/)
body1 += "\n<CONS>\n";
body1 += "<Name>${data[0]}</Name>\n";
body1 += "<value>${data[1]}</value>\n";
body1 += "<Test>${data[2]}</Test>\n";
body1 += "</CONS>";
}
println body1
//def xmlBuilder = new StringBuilder()
//xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
def xmlString = """
<data>
<CAMPAIGN>${CAMPAIGN}</CAMPAIGN>
<POLY>${POLY}</POLY>
<BATCH>${BATCH}</BATCH>
<UOM>${POLY}</UOM>
<PRODUCT>${BATCH}</PRODUCT>
<LINE>${LINE}</LINE>
<VERSION>${VERSION}</VERSION>
<TIMESTAMP>${TIMESTAMP}</TIMESTAMP>
<ABORTED>${ABORTED}</ABORTED>
<CONSUMPTIONS>${body1}</CONSUMPTIONS>
</data>
"""
xmlString = xmlString.replaceAll("null","")
message.setProperty("data",xmlString)
return message
}