Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Jan 23 at 06:53 PM

    you need odatamodel to call "create"

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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 24 at 07:10 AM

    Hi,

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

    var oModel = new sap.ui.model.odata.ODataModel(serviceURL);
    Add comment
    10|10000 characters needed characters exceeded

    • 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.

  • Jan 25 at 09:35 AM

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

    Add comment
    10|10000 characters needed characters exceeded