Search code examples
odatasapui5

Get generated ID after insert in UI5


My problem is, that I need to get the generated ID of a created entry. I want to create one entry in my ODataModel and then another one. The second one should hold the ID of the first one as property.

But when I insert the first entry with {"Id" : "0"}, so the ID gets automatically generated, I find no way to get the response Header to save the generated ID in a variable.

I tried to save the response out of the success callback, but the variable stays undefined because of the asynchronicity.


Solution

  • Meanwhile I solved the problem by myself. I moved the creation of the second entry into the success callback of the first entry. Thus I was able to get the ID out of the "response" parameter and added it as property to my second entry. That is probably not the best solution but it works for me. Maybe somebody else has another proposal to do it better.

    This is just a samplecode, but maybe it will help some people who got a similar problem.

                // oView = this.getView();
                // First Entry for EntitySampleSet1
                var oEntry = {};
    
                oEntry.Id = "0";
                oEntry.Label = oView.byId("label").getValue();
                oEntry.Status = oView.byId("status").getValue();
    
                // Success Callback for the first Entry Creation
                // response contains the Response Header for the POST Request
                var fnSuccessCallback = function(oData, response)
                {
                    console.log("Success 1");
    
                    // Success Callback for the second create methode
                    var fnSuccess2Callback = function()
                    {
                        console.log("Success 2");
                    };
    
                    // Error Callback for the second create methode
                    var fnError2Callback = function()
                    {
                        console.log("Error 2");
                    };
    
                    // Second Entry for EntitySampleSet2
                    var o2Entry = {};
    
                    // ID = "0" so the ID gets automatically generated by JPA
                    o2Entry.Id = "0";
                    o2Entry.SampleRelatedObject = response.data.Id;
    
                    // The second Entry gets created in the OData Model
                    oModel.create("/SampleEntitySet2", o2Entry,
                            {
                                urlParameters : null,
                                success : fnSuccess2Callback,
                                error : fnError2Callback
                            });
                };
    
                var fnErrorCallback = function(oError)
                {
                    console.log("Error1");
                };
    
                // The first Entry gets created in the OData Model
                // after the create worked fine the fnSuccessCallback will get 
                // called and the second entry will be created in that method
                oModel.create("/SampleEntitySet1", oEntry,
                {
                    urlParameters : null,
                    success : fnSuccessCallback,
                    error : fnErrorCallback
                });