cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

jamie_cawley
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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

jamie_cawley
Advisor
Advisor
0 Kudos

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

Regards,

Jamie

Former Member
0 Kudos

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

jamie_cawley
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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

jamie_cawley
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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

Answers (0)