cancel
Showing results for 
Search instead for 
Did you mean: 

oModel.create is not a function

chaudhary777
Discoverer
0 Kudos
197
This is my controller for adding data in backend through UI , but i am getting error oModel.create is not a function 
onOpenDialog: function () {
            if (!this._oDialog) {
                this._oDialog = new Dialog({
                    title: "Add New Product",
                    content: [
                        new VBox({
                            items: [
                                new Input("dialogInputID", {
                                    placeholder: "Enter Product ID"
                                }),
                                new Input("dialogInputName", {
                                    placeholder: "Enter Product Name"
                                }),
                                new Input("dialogInputDescription", {
                                    placeholder: "Enter Description"
                                }),
                                new Input("dialogInputReleaseDate", {
                                    placeholder: "Enter Release Date"
                                })
                            ]
                        })
                    ],
                    beginButton: new Button({
                        text: "Submit",
                        press: this.onAddProduct.bind(this)
                    }),
                    endButton: new Button({
                        text: "Cancel",
                        press: function () {
                            this._oDialog.close();
                        }.bind(this)
                    })
                });

                this.getView().addDependent(this._oDialog);
            }

            this._oDialog.open();
        },

        onAddProduct: function () {
            // Retrieve data from dialog inputs
            var oID = sap.ui.getCore().byId("dialogInputID").getValue();
            var oName = sap.ui.getCore().byId("dialogInputName").getValue();
            var oDescription = sap.ui.getCore().byId("dialogInputDescription").getValue();
            var oReleaseDate = sap.ui.getCore().byId("dialogInputReleaseDate").getValue();
       
            // Validate inputs
            if (!oID || !oName || !oDescription || !oReleaseDate) {
                sap.m.MessageToast.show("Please fill in all fields.");
                return;
            }
       
            // Create new product object
            var oNewProduct = {
                ID: parseInt(oID), // Ensure ID is an integer
                Name: oName,
                Description: oDescription,
                ReleaseDate: oReleaseDate // Format date as required
            };
       
            // Get the OData model
            var oModel = this.getView().getModel("ODataM1");
       
            if (oModel) {
                // Create a new entry in the backend
                oModel.create("/Products", oNewProduct, {
                    success: function () {
                        console.log("Product added successfully!"); // Debugging statement
                        sap.m.MessageToast.show("Product added successfully!");
                       
                        // Close the dialog
                        if (this._oDialog) {
                            this._oDialog.close(); // Close the dialog
                        } else {
                            console.error("Dialog instance is not available.");
                        }
       
                        // Refresh the table to display new data
                        var oTable = this.getView().byId("productTable");
                        if (oTable) {
                            oTable.getBinding("items").refresh(); // Refresh the table binding
                        }
                    }.bind(this),
                    error: function (oError) {
                        console.error("Error adding product:", oError);
                        sap.m.MessageToast.show("Error adding product.");
                    }
                });
            } else {
                console.error("The OData model is not instantiated or is not available.");
            }
        },

Accepted Solutions (0)

Answers (3)

Answers (3)

Bibhu
Participant
0 Kudos

HI @chaudhary777 ,

The error oModel.create is not a function that suggests that your model isn't an OData model or isn't instantiated correctly. Make sure that ODataM1 is set as an OData model

(i.e  sap.ui.model.odata.v2.ODataModel) either in your manifest.json.

 

"models": {
   "ODataM1": {
      "type": "sap.ui.model.odata.v2.ODataModel",
      "settings": {
         "serviceUrl": "/path/to/your/odata/service/"
      }
   }
}

 

or Component.js

 

var oModel = new sap.ui.model.odata.v2.ODataModel("/path/to/your/odata/service/");
this.setModel(oModel, "ODataM1");

 

Also, double-check that this.getView().getModel("ODataM1") is returning the correct model. You can verify this by logging it or using this.getOwnerComponent().getModel("ODataM1") it to access the model.

Correct Version of ODataModel: Ensure you're using the correct version of the ODataModel (v2 or v4). The create method might differ depending on the version. If you're using it, the create method is different and might require a different approach.

create operation in OData V4 Model (sap.ui.model.odata.v4.ODataModel):

 

// Assuming the OData V2 model is already instantiated and set to the view or component
var oModel = this.getView().getModel("ODataM1");

var oNewProduct = {
    ID: 101,
    Name: "New Product",
    Description: "A new product description",
    ReleaseDate: "2023-09-22"
};

oModel.create("/Products", oNewProduct, {
    success: function () {
        sap.m.MessageToast.show("Product added successfully!");
    },
    error: function (oError) {
        sap.m.MessageToast.show("Error adding product.");
        console.error(oError);
    }
});

 

create operation in OData V4.

 

// Assuming the OData V4 model is already instantiated and set to the view or component
var oModel = this.getView().getModel("ODataM1");

var oContextBinding = oModel.bindContext("/Products");

var oNewProduct = {
    ID: 101,
    Name: "New Product",
    Description: "A new product description",
    ReleaseDate: "2023-09-22"
};

// Use the `create` method in V4 via a `ContextBinding`
oContextBinding.create(oNewProduct).created().then(function () {
    sap.m.MessageToast.show("Product added successfully!");
}).catch(function (oError) {
    sap.m.MessageToast.show("Error adding product.");
    console.error(oError);
});

 

Key Differences:

  • V2: create is directly available on the ODataModel.
  • V4: You need to bind a context and then use create through the ContextBinding.

Reference : 
ODATA V4 Creating Bindings 
ODATA V4 Tutorial: Step 6: Create and Edit 
OData V2 Model 

@chaudhary777  Including a call stack is helpful when raising an issue, as it provides more context and can make debugging faster for others.

BR,
@Bibhu 

karthikarjun
Active Contributor
0 Kudos

Hello chaudhary777 - 

Could you refer the below code and blogs. I hope it will guide you some direction. 

 

let oModel = this.getView().getModel();
let oBinding = oModel.bindList("/ENTITY");
oBinding .create({
    "<YOUR ACTION>"
});

 

Reference: 

Step 6: Create and Edit - Documentation - Demo Kit - SAPUI5 SDK (ondemand.com)

Implementing CRUD Operations in OData v4 - SAP Community

Regards,

Karthik Arjun

SAP Full-Stack Development

junwu
Active Contributor
0 Kudos

var oModel = this.getView().getModel("ODataM1");  check what you get there during debug.

what's the model version? v2 or v4?

chaudhary777
Discoverer
0 Kudos
v4
junwu
Active Contributor
0 Kudos
that's the issue, for odata call, v4 is completely different from v2.
junwu
Active Contributor
0 Kudos
your code is for v2, google how to do crud with v4.