Skip to Content
0

In View Controller: OData is not defined

Oct 16, 2017 at 12:33 PM

55

avatar image
Former Member

Hi team,

Can any one please share the sample code for resolving the below mentioned issue.

In View Controller: OData is not defined

I have refereed the below mentioned blog, where Mr. Scott has got the same issue and he resolved the issue, so my request is can you please share the code.

Regards,

Swamy

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Jun Wu Oct 16, 2017 at 03:16 PM
0

there is no need to use OData object

Share
10 |10000 characters needed characters left characters exceeded
Mahesh kumar palavalli Oct 17, 2017 at 06:02 AM
0

Actually in that code he didnt get the instance of the odata service model and directly trying to access it, which is undefined. First, you need to instantiate the odata service model and then only you can use the odata crud methods.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Oct 17, 2017 at 06:25 AM
0

Hello Sharanabasappa,

Declare the oData model in manifest.json as follows

"sap.app": {
.
.
"dataSources": {
 "mainService": {
   "uri": "/ZMM_EMP_SRV",
   "type": "OData",
     "settings": {
        "odataVersion": "2.0",
         "localUri": "localService/metadata.xml"
      }
     }
  }
},


Use the above datasource (mainsService) inside models as follows (in manifest.json):

"sap.ui5": {

.
.
.
"models": {
 "backEndModel": {
 "dataSource": "mainService",
 "preload": false
 }
}

Now you can refer backEndModel in the view controller as:

var oDataModel = this.getView().getModel(backEndModel);

oDataModel.read("/EmployeeSet", {<oDataParameters>});

For more details about various operations available in oDataModel, please refer the following link(I assume you use oData model Version 2)

oDataModel (v2)

Regards,

Fahad

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

hi FAHAD HAMSA thank for your quick reply.

as i am new to this ...i dont know much about this coding.... here in my app i need to call more than one Odata service .

is it possible to declare multiple Odata services in manifest.json ?

i written below code on clicking on save button in my page.

i tried this in 2 ways ( in both cases not able to create a entry in backend system)

1st way:


var sServiceUrl = "https://xxx:5200/sap/opu/odata/sap/ZLOGIN_DATA2_SRV";
var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
// var oJsonModel = new sap.ui.model.json.JSONModel();

// oModel.read("/ZLOGIN_DATA2Set?", null, null, true, function(oData, repsonse) {
// oJsonModel.setData(oData);
// });
// sap.ui.getCore().setModel(oJsonModel);

var oEntry = {};

///// here i filled OEntry with my values

oModel.create('/ZLOGIN_DATA2Set', oEntry, null, function(odata) {
alert("Create successful");
}, function(err) {
alert("Create failed");
});

2nd way:

var sServiceUrl = "https://xxx:5200/sap/opu/odata/sap/ZLOGIN_DATA2_SRV";
var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
// var oJsonModel = new sap.ui.model.json.JSONModel();

// oModel.read("/ZLOGIN_DATA2Set?", null, null, true, function(oData, repsonse) {
// oJsonModel.setData(oData);
// });
// sap.ui.getCore().setModel(oJsonModel);

var oEntry = {};

///// here i filled OEntry with my values

oModel.read({

requestUri : "https://xxx:5200/sap/opu/odata/sap/ZLOGIN_DATA2_SRV/ZLOGIN_DATA2Set",
method: "GET",
headers : {
"X-Requested-With" : "XMLHttpRequest",
"Content-Type" : "application/atom+xml",
// "Content-Type": "application/atom+xml;type=entry; charset=utf-8",
"DataServiceVersion" : "2.0",
"X-CSRF-Token" : "Fetch"
}
},
function(data, response) {
header_xcsrf_token = response.headers['x-csrf-token'];
var oHeaders = {
"x-csrf-token": header_xcsrf_token,
'Accept' : 'application/json',
};

oModel.create({
requestUri : "https://xxx:5200/sap/opu/odata/sap/ZLOGIN_DATA2_SRV/ZLOGIN_DATA2Set",
method : "POST",
headers : oHeaders,
data:oEntry
},
function(data, request) {
alert("Employee Created Successfully");
location.reload(true);
},
function(err) {
alert("Employee Creation Failed");
});
},
function(err) {
var request = err.request;
var response = err.response;
alert("Error in Get— Request" + request + "Response" + response);
});

Can you please suggest me how to correct this.

0