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;

}