Skip to Content
0

How to get created object when using odata V4

Jun 01, 2017 at 05:56 PM

184

avatar image

Hi,

I am currently trying to figure out if oData v4 model implementation fits to our needs/ requirements but I am already struggling with adding new entities. After successful creation, I am not able to fetch the returned object, even though I can see that the server includes it in his response.

I tried to follow the Demo App in Explored but I am not able to get it to work.

My code looks like this:

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

Afterwords I am trying to access this newly created record.

            oContext.created().then(function() {
                oDialog.close();
                MessageBox.alert("Created Main Menu Item with id " + oContext.getProperty("id") + ".", {
                    icon: MessageBox.Icon.SUCCESS,
                    title: "Success"
                });
            }, function(oError) {
                oDialog.close();
                MessageBox.alert("Could not create Main Menu Item: " + oError.message, {
                    icon: MessageBox.Icon.ERROR,
                    title: "Error"
                });
            });

but I always keep getting the error "Failed to drill-down into -1/id, invalid segment: id".

What am I doing wrong?

Thanks for help,

Oliver

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

avatar image
Former Member
Aug 10, 2017 at 08:02 AM
0

Hi Oliver,

after successful POST for the create, the OData V4 model only updates the *selected* properties of the newly created entity. Hence your issue might be you did not select "id". Possible reasons

(1) you specify a $select binding parameter for this.getView().byId("mainmenulist").getBinding("items") which does not contain "id"

(2) you have switched on auto-$expand/$select but the UI does not have a binding to "id" for the entity so that the auto-generated $select does not contain "id".

Regards,
Patric

Share
10 |10000 characters needed characters left characters exceeded
Thomas Schmidt Aug 18, 2017 at 02:10 PM
0

I am facing the same problem and the cause is that the "id" is generated automatically in the backend and should not be included in the entity for the POST. but that is also the reason because we have problems to access the new created id which is included in the response in the .created method

Share
10 |10000 characters needed characters left characters exceeded
Oliver Unknown Sep 21, 2017 at 08:05 AM
0

Same situation in my case. The id is created automatically in the backend from the database server.

Is there a solution for that?

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Oliver,

this is exactly the case in the SalesOrders sample. The SalesOrderID is created only by the backend. The sample application sends an empty ID and receives the actual ID in the response. Would that or something similar not work in your case?

As outlined by Patric, the solution should be to add the id property to the $select of the binding. The id would then be fetched as well.

Best regards
Mathias.

0

Hi Mathias,

with a newer Version of UI5 it suddenly worked, issue is solved.

Sorry for not reporting back.

Regards,

Oliver

0