Skip to Content
0
Jun 18, 2021 at 07:25 AM

Issue while calling createEntry method of OData V2 model, with default TwoWay binding mode

57 Views

Hi,

I am trying to use createEntry method of Odata V2 model, to add a new entry in sap.m.table and update it in the DB.

The table items are bound to "/Products" entity set in the view.

Below is what I am doing in the controller's onAddConfirm method -

// Click OK on the create product dialog

onAddConfirm:function (oEvent) {

let addProductModel = new JSONModel({

ProductName: this.byId("idProductName").getValue(), // getting the values which are set by the user in the dialog

UnitsInStock: this.byId("idProductQuantity").getValue() // getting the values which are set by the user in the dialog

});

// creating a new column list item, to be added to the table.

let oNewItem = new ColumnListItem({

cells: [

new Input({

value: "{ProductName}",

change: this.onProductChange

}),

new Input({

value: "{UnitsInStock}",

change: this.onProductChange

})

]

});

let oTable = this.byId("productsTable"); // this is the table into which the new entry will be inserted

// add the ProductID and trigger the createEntity operation of the OData model

let oProductKey = this.getNewProductID();

oProductKey.then(newProductID=> {

// addProduct JSONModel holds the data for the newly added entry

addProductModel.setProperty("/ProductID", newProductID);

let oContext = this.oModel.createEntry("/Products", {

groupId:"productsGroup", // the batch call for createEntry is deferred and will be submitted on Save.

properties: addProductModel.getData(),

success: successCallback,

error: errorCallback

});

function successCallback(oData, oResp) {

console.log(oData);

}

function errorCallback(oError) {

console.log(oError);

}

oNewItem.setBindingContext(oContext);

oTable.addItem(oNewItem);

// close the dialog after adding item to the table

this.byId("createProductDialog").close();

// clear the fragment input values

this.clearFragmentValues();

});

}

This still works fine, as the newly created entry is getting added to the table and displayed to the user.
Changes for createEntry will be submitted to the backend, when the application will call onSubmitChanges method, on click of Save button by the user.
Now, before clicking on the Save button, if the user edits an existing entry in the table, the new entry which has been added by oTable.addItem() is getting removed.
I suspect that this is happening because I am using TwoWay binding mode as default for the OData model.
If I change the binding mode to "OneWay" as default, then the newly added entry in sap.m.table is getting persisted on the UI screen, even if the user edits an existing entry after adding the new entry.

Please let me know if the problem statement is clear, and any more info is needed.

Thanks & Regards,
Shivam