Shailesh Ingale
Shailesh Ingale

Reputation: 121

OData service not getting triggered

I have created an data service in netweaver gateway which I intend to consume in SAPUI5 app. This service works fine when I test it in backend.

In my app I have declared this service in manifest.json but I have not bound this to any UI component. Does that mean I need to trigger it manually? This might sound very basic but when is the data service actually triggered in the lifecycle of the app?

Here is an excerpt of my manifest file:

"datasources" : {
   "testODataRemote" : {
     "uri": "https://<hostname>:<port>/sap/opu/odata/SAP/<service_name>/",
     "type": "OData",
     "settings": {
       "odataVersion": "2.0"
     }
   }
}
"models": {
   "testOData": {
      "datasource": "testODataRemote"
   }
}

I my controller in one of the method I try to get data from the model as:

var oModel = this.getView().getModel("testOData");
var aData = oModel.getProperty("/entityset_name");

This does not work. My guess is that service is not triggered.

Upvotes: 1

Views: 2304

Answers (1)

Rahul Bhardwaj
Rahul Bhardwaj

Reputation: 2343

As your ODataModel has not been bound to any control to fetch the data, you will manually need to call a read method of the model. Read Method-Click here

If you bind your OData model to, say a List, it automatically calls the read method (with entity Name) of OData model and your data is stored in ODataModel.

In your case, you have just declared the model. So, it will only fetch the metadata of the OData service. Now, your OData service can have multiple entities sets. The application will not know which entity set to load. Hence, by binding an entity set to any control, it will fetch that entity set.

In the Read method of ODataModel, you specify which entity set to be loaded into your OData Model. The success handler of the read method will contain your fetched data (which will be stored in ODataModel for later use).

EDIT: read method:

oODataModel.read("/Orders"/*entity set to be fetched */, {
  success:function(oData, oResponse) {
    console.log(oData);
  },
  error: function(oError) {
    console.log(oError);
  }
});

Upvotes: 5

Related Questions