OData and SAP Netweaver Gateway. Part V. CRUD Operations in OData Services

As of now in our tutorial series, we have explored the GET Operation involving READ_ENTITY and READ_ENTITYSET methods. GET is used for pulling information and displaying. GET or READ Method is the “R” of the popular “CRUD” Operations. Others being Create, Update and Delete. In this article, we would concentrate on writing to the database using POST and PUT operations and remove entries using DELETE operation. We would be creating, updating and deleting entries in the database. And the Guinea pig remains the same, i.e. ZEKKOEKPO table.

A. POST Operation

As the name suggests, it is mainly used for creating new entries. For our stand-alone test, we cannot directly choose POST Operation and execute the URI. POST Operation is a two steps process in t-code /n/IWFND/GW_CLIENT. We need to first prepare the request string using GET Operation and then use the string to POST data.

i. Execute GET Operation with “Use as Request” option
URI: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet(Ebeln=’4580000990′,Ebelp=’00001′)

ii. Execute the POST operation

Oops, you get an error.

What did you think? Can you execute the POST operation without writing any code? It is not magic even though ABAPers are magicians.

We still have two activities to perform.
i. Redefine CREATE_ENTITY method
ii. Call the POST Operation with correct URI

Let us redefine the method and write simple ABAP code to insert entries in the custom table.

METHOD zekkoekposet_create_entity.
 
DATA: ls_zekkoekpo LIKE er_entity.
 
io_data_provider->read_entry_data( IMPORTING es_data = ls_zekkoekpo ).
 
ls_zekkoekpo-mandt = sy-mandt.
 
er_entity = ls_zekkoekpo.
 
INSERT INTO zekkoekpo VALUES ls_zekkoekpo.
 
ENDMETHOD.

Before we execute the POST Operation, let us confirm there is no entry in the custom table.

Put an external breakpoint on the CREATE_ENTITY Method to prove that it is triggered while executing POST operation.

Let us execute the POST-Operation again.

URI: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet(Ebeln=’4580000990′,Ebelp=’00001′)

You get the same error “Method Not Allowed”.

Did you mark; our action did not stop at the breakpoint. What does it mean? It means the method was not triggered at all and the URI is not the correct one.

Scroll up and check, we specifically said: “Call the POST Operation with correct URI”. So the correct URI to POST is: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet

Finally, the Method is triggered and the debugger stops at the break point.

Check the status is “Created”.

Let us validate if the entry is really populated in the custom table.

Yes!! It is.

Congrats, you just did a POST Operation in OData Service. Remember the above steps and URI options. We would need them when we consume these URIs in our SAPUI5 Applications (SAPUI5 Tutorial series to be commenced soon).

Did you take notice, there is only CREATE_ENTITY Method? No CREATE_ENTITYSET Method. Does it mean we can create just one row at a time? Absolutely not. You can always pull multiple entries and post into the database.

B. PUT Operation

While POST is to create entries, PUT Operation is to update/modify existing entries. Like POST Operation, PUT also has two steps. Execute GET Operation with the option “Use as Request” and then execute the real PUT Operation.

URI: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet(Ebeln=’4580000990′,Ebelp=’00001′)

Once we have the Request, let us execute the PUT Operation.

Were you expecting to update the table without writing any code? My friend, who will implement the Update_Entity Method?

METHOD zekkoekposet_update_entity.
 
DATA: ls_request_input_data TYPE zcl_zgw_purchase_mpc=>ts_zekkoekpo.
 
io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data ).
 
ls_request_input_data-mandt = sy-mandt.
 
UPDATE zekkoekpo FROM ls_request_input_data.
 
ENDMETHOD.

URI: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet(Ebeln=’4580000990′,Ebelp=’00001′)

Let us pass the URI to update the MENGE value of the entry which we created above.

Check we have changed the MENGE value to 14 from 7 in the Request String which would be passed to PUT Operation.

Let us execute.

The status looks green. But the text says, “No Content”. I am sure, you are as confused as me. What really happened?

When I go the table, it shows the entry was successfully updated. All is well that ends well. The status code for correct Update is ‘204’. So we are good.

Hopefully, this was enough demonstration of PUT/UPDATE Operation. If you still have doubt, just put a breakpoint at the UPDATE_ENTITY method and understand the flow. If your method is not triggered, it means your URI is incorrect. Keep this as your homework. Debug PUT Operation.

C. DELETE Operation

For DELETE we do not need to execute GET and Use as Request. We just need to pass the right URI with correct options.

Let us delete the same entry which we created and modified above.

URI: /sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet(Ebeln=’4580000990′,Ebelp=’00001′)

We have missed it the third time today. Remember, if we want to execute any Operation, the corresponding Method has to be redefined and implemented. Hopefully, in your real project, you would not miss it.

Let us redefine and implement the DELETE_ENTITY Method.

METHOD zekkoekposet_delete_entity.
 
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_ebeln TYPE ebeln,
lv_ebelp TYPE ebelp.
 
* Get the key property values
READ TABLE it_key_tab WITH KEY name = 'Ebeln' INTO ls_key_tab.
IF sy-subrc = 0.
lv_ebeln = ls_key_tab-value.
ENDIF.
 
* Get the key property values
READ TABLE it_key_tab WITH KEY name = 'Ebelp' INTO ls_key_tab.
IF sy-subrc = 0.
lv_ebelp = ls_key_tab-value.
ENDIF.
 
* Delete entry
DELETE FROM zekkoekpo WHERE ebeln = lv_ebeln AND ebelp = lv_ebelp.
 
ENDMETHOD.

Put an external break point and execute the DELETE Operation.

Check the status. It is Green with the message ‘No Content’.

The message text is a little misnomer, but this time I am not worried. I know it worked as the message code is green and the method triggered. Let us validate it.

The only entry present in the table ZEKKOEKPO has been deleted. So our DELETE Operation has executed successfully.

PS: There are two other Operations viz PATCH and MERGE. You might want to check them separately.

Hope you have a fair idea about CRUD operations now. In real work, the logic to read, edit and parse would be more complex depending on the business needs. But the fundamentals would remain the same. The corresponding methods you need to re-define and implement would remain the same. So be confident!! You can handle CRUD features of the any OData Service, however crude it might be.