cancel
Showing results for 
Search instead for 
Did you mean: 

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

Accepted Solutions (0)

Answers (4)

Answers (4)

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);

mathias_uhlmann
Advisor
Advisor
0 Kudos

Hi Oliver,

somehow getting notified did not work here five years ago. Let me post an answer nevertheless. The POST request is retried when it has failed. This happens either when the application calls submitBatch again or, in case of an Auto update group, when any property of the transient entity has changed. The idea behind this is that the user gets the backend's error messages displayed, corrects the entered values and the POST is repeated.

This retry also means that the created promise cannot be rejected after the first failed POST. The API documentation states "A promise that is resolved without data when the entity represented by this context has been created in the back end. It is rejected with an Error instance where oError.canceled === true if the transient entity is deleted before it is created in the back end, for example via sap.ui.model.odata.v4.Context#delete," accordingly.

The idea (also for OData V2) is that the backend provides information about the reason of the failure in messages. The ODataModel parses the response and puts the messages to the message model. The application is responsible to display the messages to the user (and to remove transition messages at an appropriate point in time). See also Server Messages in the OData V4 Model.

To learn that a POST request has failed, the createCompleted event may be used.

Best regards
Mathias.

mathias_uhlmann
Advisor
Advisor

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.

a0000123
Explorer
0 Kudos

But the example is refered to a list. What Oliver is trying to say, is that he ,,,( and everyone ) wants to add 2 fields, ( that could be or not in a form, because not allways you insert regiters from a form or list).

that's all. its necessary to have more practical examples by SAP.

former_member374496
Participant
0 Kudos

Hello Oliver,

could you explain to me how to create an entity with oData v4? If its possible without List binding, because i dont have a list. I use two input fields to create a new entity.

I have the same requirement as above, could you please let me know if there are any ways to achieve this ? In my case i have FeedInput, User allowed to enter the comment and post it , upon posting the comment shd get saved in the entity and we are using V4 version..

Thanks in advance,

Pradeep

Hello Pradepp,

you must create a list object in your code via:

const oListBindingVehicle = this.getView().getModel("kddietz").bindList("/Vehicle");

On these reference, you can call the function "create".

oListBindingFahrzeug.create(oVehicleDataJson, true);
mathias_uhlmann
Advisor
Advisor
0 Kudos

Hello Oliver,

could you explain to me how to create an entity with oData v4? If its possible without Listbinding, because i dont have a list. I use two input fields to create a new entity.
Thank you very much!

Do you have this answer already? I have the similar issue if you can share your solution. Thanks

former_member374496
Participant
0 Kudos

Hi marc1412,

Did you get a solution on this?