Netweaver Gateway Development

how to implement Deep Insert in SAP backend OData service?

          Deep Insert is used for creating an entity with deep data in an inlined format.ie we can send nested data to sap backend and we can update the data accordingly.For example Sales order and line item data.
           In this tutorial we will create a flight in SCARR table and its corresponding flight schedules in SPFLI table using CREATE_DEEP_ENTITY method.We will use our flight service created in earlier tutorial.
Pre-requisites


Steps
  1. Go to tcode SEGW .Now open our flight service and expand service artifacts node and find ****_DPC_EXT class and go to edit mode.Double click to open the class.
  2. From inherited methods node find /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY and right click and press redefine.
  3. Paste below code inside CREATE_DEEP_ENTITY method.You must be familiar with the coding if you had a look at earlier tutorial.We create structure with inline data and assign incoming data to the structure.After that apply some logic and update the tables.

  4.     data lv_entityset_name type string.
        data: begin of str_exp.
                include  type zcl_ztest_odata_01_mpc_ext=>ts_flight.
        data: toflightschedules type  zcl_ztest_odata_01_mpc_ext=>tt_flightschedule.
        data:    end of str_exp.
    
        data: ls_deep     like str_exp,
              ls_schedule type zcl_ztest_odata_01_mpc=>ts_flightschedule.
        data: wa_flight   type scarr,
              it_schedule type table of spfli.
    
        lv_entityset_name = io_tech_request_context->get_entity_set_name( ).
    
        case lv_entityset_name.
          when 'FlightSet'.
            io_data_provider->read_entry_data( importing es_data = ls_deep ).
            wa_flight-carrid = ls_deep-carrid.
            wa_flight-carrname = ls_deep-carrname.
            wa_flight-currcode = ls_deep-currcode.
            wa_flight-url = ls_deep-url.
            loop at ls_deep-toflightschedules into ls_schedule.
              append ls_schedule to it_schedule.
            endloop.
            insert scarr from wa_flight.
            if sy-subrc eq 0.
              insert spfli from table it_schedule.
              if sy-subrc eq 0.
                copy_data_to_ref( exporting is_data = ls_deep
                                           changing  cr_data = er_deep_entity ).
              endif.
            endif.
    
        endcase.    
     
       
    

  5. Now we can test  in gateway client( /IWFND/GW_CLIENT).Our service url look like this.http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet('AC')?$expand=ToFlightSchedules.Press execute button.We will get output of flightschedules of flight with carrid AC.

  6. Now press Use as Request button.Now we can see the details in left hand side.Now we will change the flight details to the details of the flight that has to be added to the backend.Iam changing it to details of flight with carrid AI.Change the url to http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet .Change the http method from get to post.and press execute button.If it was success you will see output like below screenshot.
Now you can check the tables SCARR and SPFLI.It will be updated with details of new flight AI.

If you enjoyed this post, Please Share!!

7 comments :

  1. Its very helpfull thanks a lot :)

    ReplyDelete
    Replies
    1. Thanks for the feedback.You are welcome.
      Regards

      Delete
  2. Your work is not in vain. thank you very much.

    ReplyDelete
  3. Replies
    1. Hi Krzysztof,
      Its is similar to create operation.Create opertaion.
      The difference is only in the entry object.
      sample code

      onCreateDeep: function(oEvent) {
      var oEntry = {
      Carrid: "",
      Carrname: "",
      Currcode: "",
      Url: "",
      ToFlightSchedules: [{
      Carrid: "",
      Connid: "",
      Countryfr: "",
      Cityfrom: "",
      Airpfrom: "",
      Countryto: "",
      Cityto: "",
      Airpto: ""

      }]
      };
      var oModel = this.getView().getModel();
      oModel.create("/FlightSet", oEntry, {
      method: "POST",
      success: function(data) {
      alert("success");
      },
      error: function(e) {
      alert("error");
      }
      });

      },

      Delete

Powered by Blogger.