Sunday 31 March 2019

Form ODATA V2 query parameters like filter,top,skip dynamically

This groovy script describes about the formation of the filter, top, skip query option in the receiver ODATA adapter for the V2 protocol.

Groovy script:


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

import java.util.HashMap;

import org.apache.olingo.odata2.api.uri.UriInfo;

import com.sap.gateway.ip.core.customdev.logging.*;


def Message processData(Message message) {   

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

    def messageLog = messageLogFactory.getMessageLog(message);

def uriInfo = message.getHeaders().get("UriInfo");

/*

The following script copies the content of UriInfo into the respective fields for OData receiver.

Reference for UriInfo:com.sap.gateway.core.ip.provider.data.UriInfoImpl.java

In OData Reciever use {header.odataEntity}?{header.odataURI}

*/

def odataURI = new StringBuilder();

def urlDelimiter = "&";

def urlConcat = "?";

def entityName = uriInfo.getTargetEntitySet().getName();

log.logErrors(LogMessage.TechnicalError, "Entity Name::"+entityName);

/*

if (uriInfo.getTop() != null){

def top = uriInfo.getTop();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$top=").append(top);

log.logErrors(LogMessage.TechnicalError, "Top value:"+top);

}

if (uriInfo.getSkip() != null){

def skip = uriInfo.getSkip();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$skip=").append(skip);

log.logErrors(LogMessage.TechnicalError, "Skip value:"+skip);

}

*/

if (uriInfo.getFilter() != null){

def filter = uriInfo.getFilter().getExpressionString();

if(odataURI.size()!=0)

odataURI.append(urlDelimiter);

odataURI.append("\$filter=").append(filter);

log.logErrors(LogMessage.TechnicalError, "Filter value:"+filter);

}

log.logErrors(LogMessage.TechnicalError, "URI value:"+ odataURI.toString());



if(messageLog != null){

messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")

messageLog.addAttachmentAsString("IncidentMappingOUTPayload:", body, "text/plain");

}

//Set Headers

message.setHeader("odataEntity",entityName);

message.setHeader("odataURI",odataURI.toString());

return message;




Wednesday 27 March 2019

Compare Date's using Groovy script in mapping in SAP CPI

If required to compare the two date's and provide the output to the field in mapping.


Groovy Script:


import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import com.sap.aii.mapping.api.*;
import com.sap.aii.mapping.lookup.*;
import com.sap.aii.mappingtool.tf7.rt.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;

/*Add MappingContext parameter to read or set headers and properties
def String customFunc1(String P1,String P2,MappingContext context) {
         String value1 = context.getHeader(P1);
         String value2 = context.getProperty(P2);
         return value1+value2;
}
Add Output parameter to assign the output value.
*/
def void custFunc2(String[] startDateJob,String[] endDateJob, String[] Field,Output output,MappingContext context) {
   
boolean out = false;
Date date1;
Date date2;
long diff;
long days;

String[] bDate;
SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");

for(int i=0;i < startDateJob.length;i++)
    {
       
              date1=formatter1.parse(startDateJob[i]); 

              for(int j=0;j < endDateJob.length;j++)
                           {

                           try{
              date2=formatter1.parse(endDateJob[j]);
              diff = date1.getTime() - date2.getTime();
              days = diff / (24 * 60 * 60 * 1000);
             
              out = (days.equals(1));
                                }
                           catch ( java.text.ParseException e)
                           { }
                          

    if (i==0 && j==0)
              {
     output.addValue(" ");
    }
   else if (days==1)
    {
     output.addValue(Field[i]);
    }}
}
}

Thanks Shruthi for the support on this script.



To split a string at a particular character using Groovy script in SAP CPI



To split a string at a particular character from the message body, utilize the script in message mapping to split.


import com.sap.it.api.mapping.*;

def void substring_CostCenter(String[] CC, Output output, MappingContext context)
{
String[] list = CC[0].split("_");

if (list.length > 0)
{
for (int j = 1; j < list.length; j++)
{

output.addValue(list[j]);
} }
}



Tuesday 26 March 2019

CSV to XML converter issue on XPATH

If you are facing an error as below in the CSV to XML converter, there is a solution for it.

java.lang.IllegalStateException: Element name [] not found in provided XML schema file


Solution:

There is an issue in the XPATH defined in the CSV to XML converter:

Path to Target Element in XSD: Root/

Instead of /Root or /Root/ use as Root/



$filter= ${in.body} in ODATA in sap cpi

In ODATA interface development, if you are facing the below issue, there is a solution for it

Error:

com.sap.gateway.core.ip.component.odata.exception.OsciException: Illegal character in query at index 162: https://XXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/IndividualCustomerCollection?$select=CustomerID&$filter=${in.body}&$top=200&$skip=0, cause: java.net.URISyntaxException: Illegal character in query at index 162: https://XXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/IndividualCustomerCollection?$select=CustomerID&$filter=${in.body}

Solution:

In this case, the filter parameter that passed is incorrect, so this data to be stored in the property value and then use it as below:

https://XXX.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/IndividualCustomerCollection?$select=CustomerID&$filter=${property.emails}



Tuesday 5 March 2019

SFTP sender channel Error: com.jcraft.jsch.sftpexception: no such file

Sender SFTP Connectivity Error in SAP CPI:

Error: com.jcraft.jsch.sftpexception: no such file

This error occurs due to the folder path selection issue. Remove the home directory path as shown below:

Path in FileZilla:  home/SRCFTP/outbound/C4C

In CPI: use this path: /outbound/C4C

Hope this resolves the issue.