Monday, 6 April 2020

Remove certain string from the query/url passing to target using groovy script in SAP CPI

Read the query parameters from the source system and remove the $filter= from it as shown below:


CamelHttpQuery from source :$filter=ContactID%3d%271925053%27%2cContactOrigin%3d%27SAP_C4C_BUPA%27

URL should be as send to target system: https://myXXX-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CONTACT_SRV;v=0003/ContactOriginData(ContactOrigin='SAP_XXX',ContactID='190000')

Query in the channel:

https://myXXX-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CONTACT_SRV;v=0003/ContactOriginData(${header.Query})

Script:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message) {

def map = message.getHeaders();

def value = map.get("CamelHttpQuery");

value = value.drop(8);

message.setHeader("Query", value);

return message;

}



Standard Character encoding change in SAP CPI

Standard Character encoding change like UTF-8 or different ISO standard formats.

Groovy script:

import com.sap.gateway.ip.core.customdev.util.Message

import java.nio.charset.StandardCharsets

def Message processData(Message message) {

def body = message.getBody(java.lang.String)

message.setBody(body.getBytes(StandardCharsets.ISO_8859_1))

return message

}



Monday, 2 March 2020

Groovy script for DateTime formatting ODATA filter





import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.util.TimeZone


def Message processData(Message message) {

//Body

def body = message.getBody();

//Properties

map = message.getProperties();

value = map.get("LastSuccessfulRun886PRD");

def curDateTime1 = "";

def tz = TimeZone.getTimeZone("Europe/Berlin")

def ts = new Date()

curDateTime1 = (ts.format("yyyy-MM-dd'T'HH:mm:ss", timezone=tz))

def datequery = "(CCSRQ_DOC_CREATED_DATE_TIME ge"+"'"+value+" CET"+"'"+" and CCSRQ_DOC_CREATED_DATE_TIME le"+"'"+curDateTime1+" CET"+"'"+")";

message.setHeader("datequery",datequery);

message.setProperty("CurrentDate",curDateTime1);

return message;

}



Thursday, 13 February 2020

POSTMAN- CSRF token validation failed error - Fix

If you encounter an issue in the POSTMAN when calling the POST method even though you are passed in the valid X-CSRF token by using the GET call in the previous step. Then here is the solution for your problem.

While sending the POST method, use the extra header parameters to solve this.

"CSRF token" and  "cookie" header should be picked from the GET call.

X-CSRF-Token:pIgKS5dokT0FZTouD8-jig==
Content-Type:application/json
Connection:keep-alive
Cookie:SAP_SESSIONID_KUH_246=fgAZqA33-I3QLvTa5yuXPb-AoFlObxHqhCcAFj5r60I%3d


Hope this resolves your issue. If yes, comment below. Thanks



Wednesday, 5 February 2020

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI.

Data:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection</id>
  <title type="text">AddressSnapshotPostalAddressCollection</title>
  <updated>2020-02-04T10:00:13Z</updated>
  <author>
    <name/>
  </author>
  <link href="AddressSnapshotPostalAddressCollection" rel="self" title="AddressSnapshotPostalAddressCollection"/>
  <entry m:etag="W/&quot;datetimeoffset'2020-01-23T12%3A51%3A32.3360950Z'&quot;">
    <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection('00163E6CDD2F1EEA8FBBE27F8D76E336')</id>
    <title type="text">AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')</title>
   <updated>2020-02-04T10:00:13Z</updated>
    <category term="cust.AddressSnapshotPostalAddress" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link href="AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')" rel="edit" title="AddressSnapshotPostalAddress"/>
    <content type="application/xml">
      <m:properties>
        <d:ObjectID>00163E6CDD2F1E7A8FBBE22F8D76E336</d:ObjectID>
        <d:ETag>2020-01-23T12:51:32.3360950Z</d:ETag>
        <d:CountryCode>BE</d:CountryCode>
        <d:CityName>Scherpenheuvel-Zichem</d:CityName>
        <d:StreetName>Bergstraat 1</d:StreetName>
        <d:StreetPostalCode>3272</d:StreetPostalCode>
        <d:CountyName/>
      </m:properties>
    </content>
  </entry>
</feed>

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
def body = message.getBody();
def data = new XmlSlurper().parseText(body);
String[] productId= " ";

def id = data.'**'.findAll { node -> node.name() == 'ObjectID' }*.text();
def country = data.'**'.findAll { node -> node.name() == 'CountryCode' }*.text();
def city = data.'**'.findAll { node -> node.name() == 'CityName' }*.text();

assert id.size() == 1;
assert country.size() ==1;
assert city.size() ==1;
message.setProperty("objectid",id);
message.setProperty("CountryCode",country);
message.setProperty("CityName",city);

return message;

}

Thanks Shruthi for helping on this solution.



Date format groovy script from UTC timezone in SAP CPI

To convert the time into the below format, we used this method for temporary fix.

Output required as : 2020-02-05 14:10:00 UTC

Groovy script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.TimeZone

def Message processData(Message message) {

//Body
def body = message.getBody();

//Properties
map = message.getProperties();

//LastSuccessfulRunTeleDev - variable value maintained in cpi system.
value = map.get("LastSuccessfulRunTeleDev");

def curDateTime1 = "";
def tz = TimeZone.getTimeZone("Europe/Berlin")
def ts = new Date()

curDateTime1 = (ts.format("yyyy-MM-dd HH:mm:ss", timezone=tz))

def datequery = "(CHANGE_DATE_TIME ge"+"'"+value+" UTC"+"'"+" and CHANGE_DATE_TIME le"+"'"+curDateTime1+" UTC"+"'"+")";

message.setHeader("datequery",datequery);
message.setProperty("CurrentDate",curDateTime1);
return message;

}





Monday, 11 November 2019

ODATA - unknown internal server error occured in SAP CPI


When access the ODATA service to the target system, it might give the error as below

Error in CPI response from receiver system:

<?xml version="1.0" encoding="utf-8"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><code>00145E5B1CC71EE0AB8B273DA099C30F</code><message xml:lang="en">In the context of Data Services an unknown internal server error occured</message></error>


Solution:

Try accessing the same link from POSTMAN tool directly to receiver system, if it gives expected response, then in CPI, delete the receiver channel and do the configuration again to resolve this issue