Friday, 18 September 2020

Script: Routing logic to send to different system based on condition in sap cpi/hci

 



Routing logic to send to different system based on condition


Groovy Script:

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

import java.util.HashMap;

import groovy.json.*;

import groovy.util.logging.*;

import org.codehaus.*;

import groovy.xml.*;




def Message processData(Message message) {

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

map = message.getProperties();




JobCategory = map.get("JobCategory");

requestid = map.get("requestid");

taskid = map.get("taskid");

message.setProperty("JobCategory1",JobCategory);

message.setProperty("requestid1",requestid);

message.setProperty("taskid1",taskid);




def route1 = "M";

def route2 = "F;

def route3 = "F;

def route4 = "M";




if(JobCategory.equals("03")) 

{

if((taskid.equals("")) && (requestid.equals("")))

{

Header = route1;

}

else

{

Header = route2;

}

}

else 

{

if((JobCategory.equals("02")) || (JobCategory.equals("01")))

{

Header = route3;

}

else

{

Header = route4;

}

}

message.setProperty("Header",Header);

return message;

}



Wednesday, 16 September 2020

One line script to remove all double quotes for integer in Json data

 It is simple to remove the double quotes from the json data for the individual values. Earlier we used to pull the value in xpath and convert that value to interger to remove the double quotes.

But it makes more easier.

Groovy Script:

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

def Message processData(Message message) {

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

// remove double quotes from all integer vaules in json data

String output = body.replaceAll("\"(\\d+)\"", "\$1");

message.setBody(output);

return message;

}



Tuesday, 15 September 2020

Unmarshalling XML error in SAP CPI while converting XML to CSV convertor


Incase if you are getting this error when use XML to CSV convertor, want to add header field name in xml to csv then by standard way it's not possible. 


  1.  uncheck use field name as header in xml to csv converter.

  2. Add content modifier after your converter. In the body part add all your header field names you want like Name,Rank,subject,etc., in first line.

 3. In second line of body add ${in.body}



Thursday, 10 September 2020

Groovy script to pick the count of days from today's date



import java.util.concurrent.TimeUnit;

import java.lang.*;

import java.time.*;

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

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;


def Message processData(Message message)

{

def map = message.getHeaders();

String getCertExpirydate = map.get("CertExpiryDate");

Date CertExpirydate = new SimpleDateFormat("yyyy-MM-dd").parse(getCertExpirydate);

Date dateNow = new Date(System.currentTimeMillis());

long dateDiff = CertExpirydate.getTime() - dateNow.getTime();

def daysToExpire = TimeUnit.DAYS.convert(dateDiff, TimeUnit.MILLISECONDS);

message.setHeader("daysToExpire",daysToExpire);

return message;

}



Thursday, 3 September 2020

Pick the xml node value and remove brackets

To pick the xml node value from the input XML which return as 

E.g: requestid => [345]

Now to remove the array brackets, also handled in script.

Now, requestid => 345 


Groovy Script:


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

import java.util.HashMap;


def Message processData(Message message) {

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

def data = new XmlSlurper().parseText(body);

def requestid = data.'**'.findAll { node -> node.name() == 'request_id' }*.text();

def taskid = data.'**'.findAll { node -> node.name() == 'task_id' }*.text();

requestid= requestid.toString().replaceAll("\\[", "");
requestid= requestid.toString().replaceAll('\\]', "");

message.setProperty("requestid",requestid);

taskid= taskid.toString().replaceAll("\\[", "");
taskid= taskid.toString().replaceAll('\\]', "");

message.setProperty("taskid",taskid);

return message;
}



Saturday, 29 August 2020

Groovy script to find the xml field and store in property

 Groovy script to find the xml field from the input payload and display the value in payload logging title in the monitoring.

Eg: 

<root>

<id>112</id>

<request_id>234</request_id>

<task_id>456</task_id>

</root>


Script:

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

import java.util.HashMap;

def Message processData(Message message) {

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


def data = new XmlSlurper().parseText(body);


def requestid = data.'**'.findAll { node -> node.name() == 'request_id' }*.text();

def taskid = data.'**'.findAll { node -> node.name() == 'task_id' }*.text();


requestid= requestid.toString().replaceAll("\\[", "");

requestid= requestid.toString().replaceAll('\\]', "");


message.setProperty("requestid",requestid);


taskid= taskid.toString().replaceAll("\\[", "");

taskid= taskid.toString().replaceAll('\\]', "");


message.setProperty("taskid",taskid);

return message;

}

Output

generally it displays as requestid as [234] and taskid as [456]

but need to remove [ ] for our requirement, so handled that aswell in script.



Friday, 28 August 2020

Error on inbound connection to C4C


Error message:


com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: Response was of unexpected text/html ContentType. Incoming portion of HTML stream: (none).

ErrorFaultCode = {http://cxf.apache.org/faultcode}server



When you facing similar kind of error, their could be some cases.

1. URL might mismatch

2. User credentials/certificate issue

3. One system uses Certificate based authentication and other uses user credentials.




Hope this solves!!