This post describes (step-by-step) how to QUERY, UPDATE, DELETE & INSERT records into any database table using following OData methods:
.
Note: The whole process is described in a
video link:
A)How to Create an OData Service
.
First of all, we have to create and configure a Project for our OData service.
1.By using TCode: SEGW → Click on “Create Project” icon.
.
2.Give a Project name & Package name. use $TMP for the local project that will not be transported.
3.Right-Click on the Data Model folder and select DDIC Structure.
.
4.Now give DDIC structure name if you know or select from list of available Structures by using F4.
.
5.Select the structure fields that are needed.
.
7.Now Save and check the project by using “Check” button.
.
.
8.Now explore the Entity Types and Entity Sets folders.
.
9.Now click on the “Generate Runtime artifacts” button.
.
.
10.Pop-Up window will show having information about Model and Service Definition.
.
11.Now provide Object Directory as required.
.
12.After SAVE the system will show that “Runtime Objects were generated successfully.”
.
b) Registration of Service:
1.For the registration of Service use Tcode “/IWFND/MAINT_SERVICE” , click on “Add Service” button.
.
1.
.
2.Give the “External Service Name” , will be find from Runtime Artifacts folder, then click on Get Services button.
.
.
.
.
.
3.Select the Service from detail section and then click “Add Selected Services” button.
4.Now Give Package Name as per requirement and click OK button.
.
5.A pop-up window will show confirming that the service registered successfully.
.
1.For the testing of Odata Service we use the Tcode “/IWFND/MAINT_SERVICE” and filter the services by using “External Service Name”.
.
.
2.Now Click on “SAP Gateway Client” button in below section after selecting the Service.
.
3.Now click on “GET” radio button and then “Execute” button.
.
.
4.If Successful, the system will show “200” in status code field of “HTTP response” section.
Its means the Service is working fine.
.
D)Implementing the custom logic in the ABAP methods.
Double Click on Runtime Artifacts.
.
.
.
2.Now explore Data Provider Extension (DPC_EXT) class → Double click on DPC_EXT class that is ZCL_ZALSK_SALE_DPC_EXT.
.
3.For querying multiple records from Database we use Get_EntitySet query Method. In order to implement our custom logic, we have to redefine the Get_EntitySet method. Right-Click on Get_EntitySet method and then select “Redefine” option.
4.Add the below code in method:
select * FROM vbak into CORRESPONDING FIELDS OF TABLE et_entityset UP TO 5 ROWS.
.
.
.
.
.
.
.
5.Activate the method and then test in GW Client. For testing Click on EntitySets button, select Entity and then execute button.
.
6.If successful, the “HTTP Response” section will show the “200” in status code field and the records in detail section.
.
.
7.For querying single record from Database we use Get_Entity query Method. In order to implement our custom logic, we have to redefine the Get_Entity method. Right-Click on Get_Entity method and then select “Redefine” option.
.
.
.
.
8.Add the following code in the method.
FIELD-SYMBOLS : <fs_keytab> TYPE /iwbep/s_mgw_name_value_pair.
*Table IT_KEY_TAB contains the Input value from Frontend & through
* ER_ENTITY parameter we can send the records back to Frontend.
UNASSIGN <fs_keytab>.
READ TABLE it_key_tab ASSIGNING <fs_keytab> INDEX 1.
IF sy–subrc IS INITIAL.
SELECT SINGLE *
FROM VBAK
INTO CORRESPONDING FIELDS OF er_entity
WHERE VBELN EQ <fs_keytab>-value.
UNASSIGN <fs_keytab>.
ENDIF.
.
.
9.Save and Activate the method.
.
.
10.Testing of GET_ENTITY method
Use Tcode /IWFND/GW_CLIENT , Click on the EntitySets button and then select the Entity.
.
.
11.In order to get a single record we have to give a key value to the Parameter.
.
12.Now when we execute the request the HTTP Response Section shows “200” in Status Code field which means the successful completion of task. It also show the particular record that were queried.
.
.
.
13.For updating a record, we use Update_Entity Method. In order to implement our custom logic, we have to redefine the Update_Entity method. Right-Click on Update_Entity method and then select “Redefine” option.
.
.
.
14.Insert the below code in the method:
.
DATA : wa_sale TYPE ZCL_ZALSK_SALE_MPC=>TS_SALES_ORDER_HEADER.
* Capture input value from Frontend into workarea WA_SCARR
CLEAR : wa_sale.
io_data_provider->read_entry_data( IMPORTING es_data = wa_sale ).
* Here we can use UPDATE or MODIFY depending on the requirement
UPDATE vbak
SET ernam = wa_sale–ernam
WHERE vbeln = wa_sale–vbeln.
IF sy–subrc IS INITIAL.
COMMIT WORK.
ENDIF.
.
15.Re-Definition of Update Entity Method:
First retrieve the required record by using Get with parameter option then click on Use as Request button.
.
.
16.Change the values of the fields as required in HTTP Request section then Select PUT option and click on execute button.
.
17.You will find the Result in HTTP response section with “200” value in status field and the changed values of Entity.
.
.
.
.
.
.
.
.
.
.
.
.
.
Re-Definition of DELETE_ENTITY method:
18.In order to delete a record, we use DELETE_ENTITY method. Right-Click on DELETE_ENTITY method and then select Redefine.
.
.
19.Insert the below code to the method
.
FIELD-SYMBOLS : <fs_keytab> TYPE /iwbep/s_mgw_name_value_pair.
*Table IT_KEY_TAB contains the Input value from Frontend & through
* ER_ENTITY parameter we use to send the records back to Frontend.
UNASSIGN <fs_keytab>.
READ TABLE it_key_tab ASSIGNING <fs_keytab> INDEX 1.
IF sy–subrc IS INITIAL.
DELETE
FROM VBAK
WHERE VBELN EQ <fs_keytab>-value.
UNASSIGN <fs_keytab>.
ENDIF.
.
.
20.Before executing the delete option first check the record in the database by using TCode SE16N
.
21.Now Get any record in SAP gateway Client by providing the value of key parameter.
.
.
22.You can see the result of GET option in HTTP Response section.
.
23.Now select the DELETE option and then click on Execute button.
.
24.You will see that “204” value shows in Status_Code field its means Record Deleted successfully.
.