Skip to Content
0

How to determine if the entity is new (created by createEntry

Oct 11 at 10:27 PM

56

avatar image

I am creating a common save routine but I need to take a different action if the entity is an existing entity on the backend or if it is a new entity created by the createEntry. Is there a way to distinguish between these in the oData model? I would think that somewhere it must know so that it can call the backend differently to update or add new.

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

1 Answer

Best Answer
Jamie Cawley
Oct 12 at 12:13 PM
0

If you are using createEntry then the model will handle this for you. All you have to do is just use submitChanges, which will submit new entries as well as edits.

https://sapui5.hana.ondemand.com/#/api/sap.ui.model.odata.v2.ODataModel

Regards,

Jamie

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

Thank you and I understand that submitChanges can handle this. However, I also need to take different actions if the entity is new or one that already exits before I call submitChanges. Something like this:

if (oEntity.isNew()) {this.logNew(entity);}

oModel.submitChanges(...);

Mike

0

You can look at the oRequestQueue which contains anything created by createEntry in the model object.

Regards,

Jamie

0

where is that found. I looked at oData model and found a property called mRequests but this is empty. Do you know where the queue is kept?

Mike

0

Oh sorry, oRequestQueue is not part of the V2 model, you should be able to use getPendingChanges to see the entries. The id will have a different format for the new entries vs the edited ones. You could also look at mDeferredRequests which contains the entire request.

Regards,

Jamie

0

That does it. With mDeferredRequests I can get the request method. A 'POST' is new and 'MERGE' is update. I don't like accessing a private property but this seems to be the best solution.

Thanks,

Mike

0

You could look at the results of getPendingChanges to do the same, the new entries most likely have a generated temporary id that is in a different format.

Regards,

Jamie

0

Yes, I considered that, however I'm creating a general purpose routine that could be used to address different object entities and I can't be certain that some entity might not have a similar format as the generated Id or that the current format for the generated id will not change in the future. I think the mDeferredRequest is more definitive so I'll go with that for now.

Thank you for your help with this.

Regards,

Mike

0