0

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?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Morten Milbak
  • 25
  • 1
  • 3
  • RetrieveMaxHit is a functionImport on the service. Can that have any impact? I tried calling with oModel.callFunction() instead of oModel.read(). Same result --> resp.data is undefined – Morten Milbak Oct 02 '15 at 09:51
  • I'm voting to close this question as it's missing details to reproduce the issue. And the current accepted answer is not only misleading but also encouraging use of deprecated API and anti-patterns. – Boghyon Hoffmann Dec 25 '20 at 23:14

2 Answers2

0

Should you not use

oModel
        .read(
            "**/**RetrieveMaxHit",

Did you also check on your gateway system with /IWFND/TRACES that the correct service is invoked and is indeed giving back the correct feedback?

Matti.b
  • 360
  • 3
  • 12
  • Thanks for your help. I posted an answer to my question with information regarding your suggestion. – Morten Milbak Oct 01 '15 at 11:44
  • Hi Morten, it might have to do with asynchronous processing. By the time you are putting your variable in maxHitData it might not have been retrieved yet. Therefore returning the variable might seems empty. Perhaps you can put it in a local model just to see if indeed the data is sent back. – Matti.b Oct 01 '15 at 11:56
  • I have tried alot regarding asynchronous/synchronous calls. The outcome is still the same. No data in response. I suspect that the issue must be regarding the format of the returned data. Can namespaces have any impact? – Morten Milbak Oct 02 '15 at 08:59
-2

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);
Morten Milbak
  • 25
  • 1
  • 3