CRUD Operations in Odata – Part V – Read Operation(Single & Multiple Records)
[vc_row penci_show_desk=”1″ penci_show_tablet=”1″ penci_show_mobile=”1″ parallax_content=”parallax_content_value” fadeout_row=”fadeout_row_value”][vc_column][vc_column_text]
OData protocol defines conventions for all of these CRUD operations or CRUD-Q (“Q” is for query) .
The Create operation is used whenever you 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.
The Read operation is the most frequently used operation.
The related HTTP method for the Read operation is GET. A successful Read operation has the HTTP 200 (OK) status code.
The Update operation is used whenever you want to change an existing entity. The HTTP method for the Update operation is PUT. If the Update operation is successful, you receive the HTTP 204 (no content) status
[penci_blockquote style=”style-3″ align=”none” author=””]Instead of PUT, it’s possible to leverage the PATCH operation that allows you to perform a partial update. The PATCH method is supported by the framework and first calls the GETDETAIL method to retrieve all properties that won’t be updated, then merges those values with the ones that are sent via the PATCH request, and finally performs an update using the standard update method of an entity set.[/penci_blockquote]
Finally, the Delete operation is used whenever you want to delete an entity. Equal to the Update operation, you have to provide the primary key of the entity you want to erase. The related HTTP method is DELETE.
A successful Delete operation has the HTTP 204 (no content) status code.
Via the sap:deletable annotation, you can specify whether entities of
an entity set can be deleted or not.
Read Operation (Multiple Records)
First you create two tables ZEKKO12 & ZEKPO12 as mentioned below
Run the tcode segw-> Create new project.
Select data model and import from the DDIC Table as shown below.
Enter ZEKKO12 as abap structure and click on next button.
Select the fields you want and click on next button.
Select at least 1 Key and Click on Finish button.
Click on Generate Runtime Objects as shown below.
After that following classes will be created.
Double click on ZCL_ZASHU_DEMO1_DPC_EXT Class and place cursor on ZEKKO12SET_GET_ENTITYSET and click on redefine button .
Write the code as shown below and activate it. Go back two times.
SELECT * FROM zekko12 INTO CORRESPONDING FIELDS OF TABLE et_entityset.
Click on Register Button as shown below to register the service for the first time.
Select System Alias as Local.Click on local object and continue.
After successful registration the status will be GREEN.
Now Click on Gateway Client Button.
Select the Entity Set and select HTTP Method as GET and Click on execute button as shown below.
You will get the entries as shown below.
Read Operation (For Fetching Single Record)
Redefine the method ZEKKO12SET_GET_ENTITY.
Write the below code and activate.(Read operation)
DATA:wa_key LIKE LINE OF it_key_tab.
DATA:lv_ebeln TYPE ekko-ebeln.
READ TABLE it_key_tab INTO wa_key WITH KEY name = ‘Ebeln’.
IF sy-subrc EQ 0.
lv_ebeln = wa_key-value.
SELECT SINGLE * FROM zekko12 INTO CORRESPONDING FIELDS OF er_entity
WHERE ebeln = lv_ebeln.
Incorrect QUERY -> /sap/opu/odata/SAP/ZASHU_DEMO1_SRV/ZEKKO12Set(Ebeln=’450000007′)
As you can see that the record 0450000007 is present with leading zero so in the URI you need to pass proper value as shown below otherwise the error 404 will come.
Correct QUERY -> /sap/opu/odata/SAP/ZASHU_DEMO1_SRV/ZEKKO12Set(Ebeln=’0450000007′)