Search code examples
sapui5abapsap-fiorisap-gateway

submitChanges() with Deep Entity?


I have an odata with a navigation set. For example, Order entityset with Items entityset. Items entityset is a navigation set (subset) of Order. It's a one to many relationship. Each order may have 0 or many items.

My sap ui5 page allows people to update their existing orders and add items to their orders and then press Submit button on screen to submit changes to gateway sap backend.

Order entityset is bound to a table control and Items entityset is bound to a dialog in a fragment. Order table has rows and each row has a button called manage items. If you press manage items button of order#1, dialog opens up with the existing items + you can add additional items to order#1. Lastly, user has to close items dialog and press submit changes. Submit changes is supposed to submit all orders and their items embedded (nested) in each order to gateway and let gateway handle the deep entity structure.

I want to be able to submit the Orders along with their Items all at once via a single oModel.submitChanges() call and the gateway will handle my request as deep entity.

I already have the backend code, I already have the gateway, test data, etc. I just need to know how to submit changes with deep entity from sap ui5.

The gateway will take care of deep entity CRUD in a sense that items will be inserted, updated, removed only and only if order transaction doesn't fail. If order transaction fails, items insertion/updation/deletion will rollback. Orders and items are heavily correlated thst us why I do not want to submitChanges for Orders seperately from Items. Instead, I want to submitChanges as deep entity that is nest items in orders and then submit all together.

Can someone kindly help how to submit changes with a deep entity from sap ui5?


Solution

  • The correct way to do this is to implement the CREATE_DEEP method on your backend. This way you can send the complete deep entity on just one request.

    The object sent will be for example:

    {
      "OrderId" : "123",
      "Kunnr" : "001234566",
      "Items": [
        "Matnr" : "123",
        "Qnt" : 1
       ]
    }
    

    On the frontend you just need to call the create method on the Order entity and everything will be taken care by the framework and by the Gateway.

    Of course you need to define on your OData service the correct association between Order and Item entities.

    This will create one request for each order that you sent (along with their respective items).

    If you need to send more than one order at a time, I would create a new entity just to aggregate all the orders in one:

    {
      "id" : "12", 
      "Orders": [{
      "OrderId" : "123",
      "Kunnr" : "001234566",
      "Items": [
        "Matnr" : "123",
        "Qnt" : 1
       ]
    }, {
      "OrderId" : "987",
      "Kunnr" : "001234566",
      "Items": [
        "Matnr" : "987",
        "Qnt" : 3
       ]
    }]
    

    This will also call the CREATE_DEEP method on the backend where you can process all the information at once and return just one result.

    P.S.: If you use the first approach, you can also send multiple request in one (using batch) but you have to process the result individually for each one. It depends if you want an "all or nothing" or "if ok insert and only repeat for the nok"