Netweaver Gateway Development

Create data in SAP Backend using OData Service

Pre-requisites

1.SAP user id with developer access
2.abap skill 
3.Odata service created using SEGW.(If you dont know how to Please refer  my previous post step-by-step-gateway-odata-service)
4.GET_ENTITYSET implemented already.(If you dont know how to Please refer  my previous post http://www.techippo.com/2015/10/odata-service-implementation-for.html)
5.GET_ENTITY method implemented already(Refer OData Service Implementation for GET_ENTITY)
 
                The Create operation is used when we want to create data on the backend server.The related HTTP method is POST. If the Create operation is successful, you receive the HTTP 201 (created) response code, along with the entity that was created. Via the metadata, you can specify whether an entity set is creatable or not.
                     In this tutorial we will use our flight service which was created in earlier tutorials.We will create a new record in our SCARR table via OData service.

Steps

  1. Open the service and click on edit button.Expand Data Model->Entity Sets and select our FlightSet.Then tick mark Creatable checkbox .Meaning is the entityset is now creatable.Dont forget to hit Generate Button.
  2. Expand Service Implementation node.Right click on Create and click on Go to Abap Workbench.
  3. You will get an information message that method is not implemented. Click OK.
  4. You will now land on class builder with ****_DPC_EXT opened.We write our code in this class.Expand methods node.expand inherited methods node.Right click on FLIGHTSET_CREATE_ENTITY and click redefine.
  5.  Paste the code to access the flight details and create new record in SCARR table inside method(FLIGHTSET_CREATE_ENTITY) and activate it.Using the read_entry_data method of the import object reference io_data_provider, you can retrieve the data that was passed along the POST request in the HTTP body. The entry is retrieved in the format of the entity type definition.Now you can insert the record in SCARR table.
  6.     data: ls_flight like er_entity.
        io_data_provider->read_entry_data( IMPORTING es_data = ls_flight ).
    
        INSERT INTO SCARR VALUES LS_FLIGHT.
    
        IF sy-subrc eq 0.
          er_entity = ls_flight.
        endif.
    
    
    

  7. Create operation implemented.Now you can test it.For testing open tcode /IWFND/GW_CLIENT.Paste url(/sap/opu/odata/sap/ZTEST_ODATA_SRV_01/FlightSet('AC')) and select GET http method then click execute button. You will get details of flight with carrid AC.
  8. Now Click on Use as Request button in the right side.This is for copying the format of our entry data.Edit the data Select POST http method.Paste the url (/sap/opu/odata/sap/ZTEST_ODATA_SRV_01/FlightSet) and press execute button.You will get a 201 created response.
If you enjoyed this post, Please Share!!

4 comments :

  1. Hello,

    I have a question regarding POST testing. Is there some way to test it in browser instead of SAP GW Client? Browser extensions are also restricted.

    Best regards,
    Kirill.

    ReplyDelete
    Replies
    1. Hi,
      You can test it using rest client from browser.If browser extensions are also restricted....I think only way to test is to create an sapui5 application and run in browser.Did you see this post create odata operation in sapui5
      Regards

      Delete
  2. Hello Rajeesh,
    Thank you for the prompt answer, I already find a way to get across browser extension restrictions, and complete the tests. Thank you for the idea with the sapui5 app also.

    There is one more issue I am struggling to resolve. Maybe you can help me or send me in the right direction to find some info.
    So I have an entity type that contains properties(fields) from different db tables. So when executing POST I should send new values to multiple different table. The example above is describing how to do it with one table.
    Maybe you have some examples regarding my issue?

    Thank you!

    ReplyDelete
    Replies
    1. Hi,
      If my understanding is correct you may be asking about updating two related tables with one single request.for example sales order and its line items.Take a look at How to implement deep entity method
      Thanks

      Delete

Powered by Blogger.