Skip to Content

oData V4 create entity

Hi,

how do I create a new entity with oData V4 model without having an existing ListBinding?

I have a SimpleForm and want to create a new entity based upon the entered information.

So I tried to manually create a ListBinding as this seems to be the only way to create a new enitty via create() method.

var oListBinding = oModel.bindList("/Templates");
var oContext = oListBinding
    .create({"id":"1"});

But nothing is being sent to the backend.

What am I doing wrong?

All examples I have found base upon a control being bound and used a syntax like this

var oListBinding = this.getView().byId("mainmenulist").getBinding("items");
var oContext = oListBinding
    .create(oMainMenuItem);

Thanks

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Apr 05 at 12:17 PM

    Hi Oliver,

    the request is created only when the respective updateGroup is submitted. Please see the

    Sales Orders sample in the demokit for an example how the creation of a new record could be handled. The request is sent to the backend when the user saves.

      onSaveSalesOrder : function () { 
        this.submitBatch("SalesOrderUpdateGroup"); 
      },
      /**
       * Submits the given batch group while the view is locked.
       *
       * @param {string} sGroupId
       *   the group ID
       */
      submitBatch : function (sGroupId) {
       var oView = this.getView();
       function resetBusy() {
        oView.setBusy(false);
       }
       oView.setBusy(true);
       oView.getModel().submitBatch(sGroupId).then(resetBusy, resetBusy);
      }
    

    Best regards
    Mathias.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 10 at 10:50 AM

    Hi Mathias,

    thanks, I now got it to work, adding entities works fine.

    But unfortunatelly I'm unable to process potential errors as the Error Part is never executed.

                var oListBinding = oModel.bindList("/Templates", undefined, undefined, undefined, { $$updateGroupId: "AddTemplate" });
                var oContext = oListBinding
                    .create({
                        "global_tool_id": odd_tools.getSelectedKey(),
                        "template_name": otxt_template_name.getValue()
                    });
    
                oContext.created().then(
                    function () {
                        MessageBox.alert(oView.getModel("i18n").getResourceBundle().getText('AddTemplateSuccessMessage') + ' ' + oContext.getProperty("id") + ".", {
                            icon: MessageBox.Icon.SUCCESS,
                            title: oView.getModel("i18n").getResourceBundle().getText('Success')
                        });
                    }, function (oError) {
                        console.log("oError");
                        console.log(oError);
                        MessageBox.alert(oView.getModel("i18n").getResourceBundle().getText(oError.message), {
                            icon: MessageBox.Icon.ERROR,
                            title: oView.getModel("i18n").getResourceBundle().getText('Error')
                        });
                    });
                oModel.submitBatch("AddTemplate").then(resetBusy, resetBusy);

    Add comment
    10|10000 characters needed characters exceeded