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: 

When the web service response comes dynamically for every request made, certain fields can be populated dynamically because of which response mapping cannot be done completely via mapping tool. Only static fields can be mapped using mapping editor and the dynamic fields can be mapped via custom scripts.

Steps from Design Time tool

  1. Create an OData Service Implementation Project
  2. Create an OData Model for an operation

      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 operation from the list of soap operations and click on finish

     6. Right click on Query and Select Define ResponseMapping

     7. Map all the static fields as below

Here the fields under businessKeys property are populated only after a query request is fired. So, these dynamic fields are mapped via scripts

      8. Right click on Query and select Define Custom Code

     9. Get the entire response from message body, extract the dynamic field values and create a list of hashMaps as below. Set this list to custom header

function processResponseXML(message) {
importPackage(java.util);
importPackage(java.lang);
var payload = message.getBody().toString();
importPackage(com.sap.gateway.ip.core.customdev.logging);
log.logErrors(LogMessage.TechnicalError, payload.toString());
var tokens = payload.split("(?=<)|(?<=>)"); var list = new ArrayList();
var innerMap = new HashMap();
for (var i = 0; i < tokens.length; i++) {
  if (tokens[i].contains("firstName")) {
   innerMap.put("firstName", tokens[i + 1]);
   i = i + 2;
  }
  else if (tokens[i].contains("lastName")) {
   innerMap.put("lastName", tokens[i + 1]);
   i = i + 2;
  }
  if (innerMap.containsKey("firstName") && innerMap.containsKey("lastName")) {
   list.add(innerMap); // Create a list of hashMaps
   innerMap = new HashMap();
  }
}
message.setHeader("MappingOutput", list); // Set the list of hashMaps to a
            // header
return message;
}


   10. From the previously set header get the list of hashmaps and from the message body get the list of hashmaps for all static fields which were mapped using mapping editor, combine them into a single list of hashMaps as below

function processResponseData(message) {
importPackage(java.util);
var data = message.getHeaders().get("MappingOutput");// Get the data from
               // previously set
               // header
importPackage(com.sap.gateway.ip.core.customdev.logging);
log.logErrors(LogMessage.TechnicalError, data);
var body = message.getBody();
var newData = null;
var newList = new ArrayList();
for (var i = 0; i < data.size(); i++) {
  newData = new HashMap();
  var dyData = data.get(i);// Getting dynamic data from the previously
         // set header
  var mappedData = body.get(i); // Getting the mapped data from mapping
          // tool
  newData.putAll(dyData);
  newData.putAll(mappedData);
  newList.add(newData); // Finally create a list of hashMaps
}
message.setBody(newList);
return message;
}

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

Now fire an OData Request https://localhost:8083/gateway/odata/SAP/SAMPLE;v=1/QuerySet on the browser.

5 Comments