on 03-30-2018 2:53 PM
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
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
In the meantime there is also a related blog post: https://blogs.sap.com/2022/06/07/ui5-odatamodel-v4-vs-v2-custom-odata-requests/.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you have this answer already? I have the similar issue if you can share your solution. Thanks
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.