Search code examples
odatasapui5sap-gateway

OData response does not contain data


I am developing a SAPUI5 application that uses the SAP OData service CRM_OPPORTUNITY.

In my program I am trying to do the following request to the OData service

getMax: function(oEvent) {      
    var oModel = this.getOpportunityODataService();
    var maxHitData;

    oModel
        .read(
            "RetrieveMaxHit",
            null,
            null,
            false,
            function(oData, resp) {
                maxHitData = {
                    RetrieveMaxHit: resp.data.results[0]
                };

            });
   return maxHitData;
},

getOpportunityODataService : function(){        
    var oModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/CRM_OPPORTUNITY/");
    oModel.forceNoCache(true);      
    return oModel;
},

The response to this request does not contain any response Data. resp.data is undefined.

If I do the request in a browser I get the following response

<d:RetrieveMaxHit xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:type="CRM_OPPORTUNITY.MaxHit">
<d:MaxHitNumber>100</d:MaxHitNumber>
<d:ActionResult>X</d:ActionResult>
</d:RetrieveMaxHit>

I hope someone can help me grasp why resp.data does not contain the data returned from the sevice? What am I missing?


Solution

  • Problem was that the oDatamodel had to be instantiated with JSON set to TRUE. Apparently the response in XML returned when calling the FunctionImport was not able to be correctly mapped into the response.Data.

    var parameters = {
        json: true
    };
    
    var oModel = new sap.ui.model.odata.ODataModel("http://XXXXX:XXXX/sap/opu/odata/sap/CRM_OPPORTUNITY/", parameters);