cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

oModel.create is not a function

chaudhary777
Discoverer
2,263
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.");
            }
        },
View Entire Topic
karthikarjun
Active Contributor

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