Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
tejesvi_dvr
Explorer

In odata - > soap, inline count operation is supported provided there is a web service operation which can return the In Line count of the No of Records from response. The operation must take an input parameter and return the count of the entries from the Web Service response.

Steps from Design Time tool

  1. Create an OData Service Implementation Project
  2. Create an OData Model for getSalesOrder operation, which can have properties CustomerID, SalesOrderID, Note etc.

         

    3. Right click on odatasvc and choose Select data source

    4. Select the entity set and choose the query CRUD operation. Select the data source SOAP Service.

      

    5. Specify the wsdl file and choose the salesOrderInlineCount operation from the list of soap operations and click on finish

       

    6. Right click on Query and select Define Custom Code

    7. Select the script type as either javascript or groovy script

       

     8. The Customer ID on which the Inline count has to be fetched. A hash map must be created in processRequestData function with key being CustID or any entity with which Web service Returns the response.

set the Hash Map to message body.


function processRequestData(message) {
  importPackage(com.sap.gateway.ip.core.customdev.logging);
  importPackage(com.sap.gateway.ip.core.customdev.util);
  importPackage(org.apache.olingo.odata2.api.uri);
  importPackage(java.util);
  importPackage(com.sap.gateway.core.ip.component.commons);
  var saleOrderInlineCount = new LinkedHashMap();
  saleOrderInlineCount.put("key:CustID", "1");
  message.setBody(saleOrderInlineCount);
  return message;
}


If the odata request ishttps://<localhost:8083/Namspace>/<ProjectName>;v=1/SalesOrder_InlineCountSet

Then the soap request body looks like


<soapenv:Body>


<web:saleOrderInlineCount>


<web:CustID>1</web:CustID>


</web:saleOrderInlineCount>


</soapenv:Body>







    9. Once the web service response is returned, the response includes both the Inline Count Value and the Payload i.e. Each row of entity set. This has to be Parsed to get the inline count value and the Normal Response.

Parsing of the Response which is in the form of an XML can be achieved using Custom Processor below is the sample code for the same.

here we are fetching the Inline Count Value by splitting the tag <inlineCount> from xmlBody which we got from Input Parameter message.

if Inline Count Value is not null we are setting a property value to message object with Key as InlineCount and the value with the value of inline Count we got from response.This property will be converted to a count value in the final odata Response.

Now we will Map the Response to EDMX Entities/Properties i.e. we are achieving this with out Design Time Tool.

create a String buffer and append the xml tag, entity set tag, entity tag, properties to it.

Proprieties are fetched by Iterate through the xmlBody to the corresponding Node level and if the node name matches the EDMX property name then append the value to the property tag.

Iterate until the next element is not found.

set the XML created using String Buffer to the message Body.


function processResponseXML(message) {
  importPackage(java.util);
  importPackage(java.lang);
  importPackage(com.sap.gateway.ip.core.customdev.util);
  importPackage(com.sap.gateway.ip.core.customdev.logging);
    importPackage(org.apache.olingo.odata2.api.uri);
    log.logErrors(LogMessage.TechnicalError, "This is first log");
 
    uriInfo = getUriInfo(message);
    var xmlBody = message.getBody().toString();
    var splitArray = xmlBody.split("<inlineCount>");
    var inlineValue = splitArray[1].split("</inlineCount>")[0];
 
    if (inlineValue != null)
    {
    message.setProperty("InlineCount", Integer.valueOf(Integer.parseInt(inlineValue)));
    //message.setProperty("InlineCount", inlineValue);
    log.logErrors(LogMessage.TechnicalError, "inlineCount:"+ inlineValue);
    }
    var payload = message.getBody().toString();
  importPackage(com.sap.gateway.ip.core.customdev.logging);
    var tokens = payload.split("(?=<)|(?<=>)");
  var buffer = new StringBuffer();
  buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  buffer.append("<SalesOrder_InlineCountSet><SalesOrder_InlineCount>");
  for(var i=0;i<tokens.length;i++)
  {
  if(tokens[i].contains("CustomerID"))
  {
  buffer.append("<CustomerID>");
  buffer.append(tokens[i+1]);
  buffer.append("</CustomerID>");
  i=i+2;
  }
  if(tokens[i].contains("Note"))
  {
  buffer.append("<Note>");
  buffer.append(tokens[i+1]);
  buffer.append("</Note>");
  i=i+2;
  }
  if(tokens[i].contains("CurrencyCode"))
  {
  buffer.append("<CurrencyCode>");
  buffer.append(tokens[i+1]);
  buffer.append("</CurrencyCode>");
  i=i+2;
  }
  if(tokens[i].contains("NetAmount"))
  {
  buffer.append("<NetAmount>");
  buffer.append(tokens[i+1]);
  buffer.append("</NetAmount>");
  i=i+2;
  }
  if(tokens[i].contains("TaxAmount"))
  {
  buffer.append("<TaxAmount>");
  buffer.append(tokens[i+1]);
  buffer.append("</TaxAmount>");
  i=i+2;
  }
  if(tokens[i].contains("BusinessPartnerID"))
  {
  buffer.append("<BusinessPartnerID>");
  buffer.append(tokens[i+1]);
  buffer.append("</BusinessPartnerID>");
  i=i+2;
  }
  if(tokens[i].contains("SalesOrderID"))
  {
  buffer.append("<SalesOrderID>");
  buffer.append(tokens[i+1]);
  buffer.append("</SalesOrderID>");
  i=i+2;
  }
  if(tokens[i].contains("Type"))
  {
  buffer.append("<Type>");
  buffer.append(tokens[i+1]);
  buffer.append("</Type>");
  i=i+2;
  }
  }
  buffer.append("</SalesOrder_InlineCount></SalesOrder_InlineCountSet>");
  message.setBody(buffer.toString());
  return message;
}

12. Right Click on Project and select Generate and Deploy Integration Content. This will deploy the bundle.

Now fire an OData Requesthttps://<localhost:8083/Namspace>/<ProjectName>;v=1/SalesOrder_InlineCountSet on the browser and response will give the list of SalesOrder Items and the InlineCount of the Items.