Create data in Backend using OData Service

1
4598

In our last tutorial Update data in Back-end using OData service you have update the data which is already existed in back-end. For SAP Netweaver Gateway tutorial click here. In this tutorial we will create new data in back-end system. We are going to use a different service from our earlier tutorials. We are going to create material in back-end system. Before creating you have created a service in service builder which will get the Product List and Product Detail.

1. Go to Service Builder SEGW. Open the project in which you want to implement CREATE operation.

c1

2. Before going further make sure you have implement GetEntitySet and GetEntity methods in the service to get List of Material and details of a single material. if you are not please go through these tutorials sap netweaver gateway tutorials.

3. Now expand Service Implementation node and select and right click on “Create” then after choose Go To Workbench.

c2

4. You will be navigated to DPC Extension class. Put the class in change mode and from the list of methods choose PRODUCTSET_CREATE_ENTITY method and redefine it.

c3

5. Write the below code in the method.


>> Source Code

METHOD productset_create_entity.

DATAlwa_headerdata TYPE bapi_epm_product_header,
lwa_product    LIKE er_entity,
lt_return     TYPE STANDARD TABLE OF bapiret2.

*1.Get new data from service
io_data_provider->read_entry_dataIMPORTING es_data lwa_product ).

lwa_headerdataproduct_id lwa_productproduct_id.
lwa_headerdatacategory lwa_productcategory.
lwa_headerdataname lwa_productname.
lwa_headerdatasupplier_id lwa_productsupplier_id.
lwa_headerdatameasure_unit ‘EA’.
lwa_headerdatacurrency_code ‘EUR’.
lwa_headerdatatax_tarif_code ‘1’.
lwa_headerdatatype_code ‘AD’.

*2.Call BAPI to create material
CALL FUNCTION ‘BAPI_EPM_PRODUCT_CREATE’
EXPORTING
headerdata               lwa_headerdata
TABLES
return                   lt_return.
IF lt_return IS INITIAL.
*3.If no errors in creattion, pass back the data
er_entity lwa_product.
ENDIF.
ENDMETHOD.

<< Source Code


 

6. Activate the DPC Extension class an methods. We have implemented the method.Lets test whether our service creates material in back end system or not.

7. To test the service got SAP Netweaver Gateway Client.CREATE operation needs an full and proper HTTP instead manually adding the HTTP body lets copy it from the result of GetEntity(Read) methods. First call any single material by using the below URI to get the HTTP body.

/sap/opu/odata/SAP/ZSL_EPM_DEMO1_SRV/ProductSet(‘AD-1000’)

8. Once you have received the result.Click on Use as Request to copy the HTTP body to left panel in gateway client.

c4

9. Once the HTTP body is copied and provide the material details to be created and change the HTTP request method from GET to POST and execute the service with below URI.

/sap/opu/odata/SAP/ZSL_EPM_DEMO1_SRV/ProductSet

c5

10. You should get the repose 201 if the material is created in back-end.

c6

11. To test that, you can again retrieve the data using the below URI adding the Product ID to the service and execute.

/sap/opu/odata/SAP/ZSL_EPM_DEMO1_SRV/ProductSet(‘AD-2001’)

  • I see your post very well!.

    I have some questions for making OData Service and Handle return object.

    In source Code 3. You passed ‘ lwa_product’ to ‘er_entity’.
    I think ‘lwa_product’ have material data and ‘er_entity’ will be used in Eclipse.
    So, I wonder How to Handle ‘er_entity’ in Eclipse.

    Thank you and Have a nice day.
    I`m Korean, SoI am not good at English. I wish you understand my Comment well 🙂