cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 oModel.create not a function [sapui5 with odata service]

0 Kudos

oModel.create is not a function

I am trying a basic CRUD operations example with sapui5 and odata.

Issue: Whenever i call oModel.create function it gives error: oModel.create... is not a function

onInit: function() {
  var serviceURL = 'http://services.odata.org/V4/OData/OData.svc/Persons?$expand=PersonDetail'; 
  var oModel = new sap.ui.model.json.JSONModel(serviceURL);
  oModel.attachRequestCompleted(function(oEvent){
  sap.ui.getCore().setModel(oModel);
});
}



/**************/
var oData = {
		Name: '',
		PersonDetail:{
			Age: '',
			Phone: '',
			Address: {
				Street: '',
				City: '',
				State: '',
				ZipCode: '',
				Country: ''
			}
		}};
		oData.Name = sap.ui.getCore().byId('sNameInput').getValue();
		oData.PersonDetail.Age = sap.ui.getCore().byId('sAgeInput').getValue();
		oData.PersonDetail.Phone = sap.ui.getCore().byId('sPhoneInput').getValue();
		oData.PersonDetail.Address.Street = sap.ui.getCore().byId('sStreetInput').getValue();
		oData.PersonDetail.Address.City = sap.ui.getCore().byId('sCityInput').getValue();
		oData.PersonDetail.Address.State = sap.ui.getCore().byId('sStateInput').getValue();
		oData.PersonDetail.Address.ZipCode = sap.ui.getCore().byId('sZipCodeInput').getValue();
		oData.PersonDetail.Address.Country = sap.ui.getCore().byId('sCountryInput').getValue();
		console.log(oData);
		var oController = sap.ui.getCore().byId('masterPage').getController();

var oController = sap.ui.getCore().byId('masterPage').getController();
		var oModel = sap.ui.getCore().getModel();
oModel.create('/value',oData,null,{success: oController.fnSuccessHandler(), error: oController.fnErrorHandler()});

All the parameters passed to the create function are right still it is showing that create is not a function. Please help.. Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

junwu
Active Contributor
0 Kudos

you need odatamodel to call "create"

you have a jsonmodel now, which will not do the job you intended.

NabiZamani
Contributor
0 Kudos

@jun.wu5 is right, his answer should be marked as correct. Additionally, I suggest you to learn the basics of UI5 by reading the Walkthrough Tutorial. It covers some OData basics as well. Make sure you understand the difference between the ODataModel and the JSONModel!

0 Kudos

Thank you jun.wu5 what if i had to use JSONModel and create entry in the service ?

junwu
Active Contributor
0 Kudos

had to?

who is forcing you???

Answers (2)

Answers (2)

ericci
Active Contributor

Hi Swapnil Lahire the problem is that you're using the JSONModel. Those methods are only supported by ODataModel (you should use v2).

irfan_gokak
Contributor
0 Kudos

Hi,

change the model from json to odata model. add below line.

var oModel = new sap.ui.model.odata.ODataModel(serviceURL);
NabiZamani
Contributor

The sap.ui.model.odata.ODataModel has been deprecated long time ago. Instead use sap.ui.model.odata.v2.ODataModel, or if you can sap.ui.model.odata.v4.ODataModel. As you can see in the code abopve an V4 OData service is called, thus sap.ui.model.odata.v4.ODataModel should be used here.