In Part 3, different artifacts in OData service are covered. In this part, we will check the implementation of the OData service and its Get Operation. As we know all the logic parts of the OData service are to be implemented in the DPC_EXT class. In the DPC_EXT class, there are a couple of methods created while generating the Project. Let’s check methods that are created for entity set.
To navigate to the DPC_EXT class there are two ways one is from Tcode SE24 or SE80 and the second way is directly from the project itself as shown below
In the class, you can see there a couple of methods, we will cover most of them in the due course. But for now, focus on the methods which are associated with the entity set.
As a whole, there are 5 operations(CRUD-Q) in OData that are as shown below and mapped to HTTP methods. But for now, we will check the GET Operation which is also known as single read operation.
Read operation is used for Reading a single record. To implement the same we need to redefine the method called ORDERHEADERSET_GET_ENTITY.
After redefining the method- the following code implemented
METHOD orderrheaderset_get_entity.
DATA:lv_vbeln TYPE vbak-vbeln.
"Get the Key value
lv_vbeln = VALUE #( it_key_tab[ name = 'Vbeln' ]-value OPTIONAL ). "New syntax
lv_vbeln = |{ lv_vbeln ALPHA = IN }|.
*** If you are using Old syntax Then-Begin
* READ TABLE it_key_tab INTO DATA(ls_key_tab) WITH KEY name = 'Vbeln'.
* IF sy-subrc EQ 0.
* lv_vbeln = ls_key_tab-value.
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* input = lv_vbeln
* IMPORTING
* output = lv_vbeln.
* ENDIF.
**** End
"Fetch the record from the table
SELECT SINGLE vbeln
erdat
erzet
ernam
audat
netwr
waerk
vkorg
vtweg FROM vbak
INTO CORRESPONDING FIELDS OF er_entity
WHERE vbeln = lv_vbeln.
ENDMETHOD.
If you check in the above code, I have highlighted it_key_tab and er_entity. These two parameters play a major role.
- it_key_tab = importing parameter which holds the KEY fields in the form of name-value pair which are passed from the URI. Note that the name should be same as the entity type property name (case sensitive)
- er_entity = exporting parameter of the method. Which to be filled by this method and sends back as a response.
If you are confused with the new syntax used in the above code then please read the blogs
Read Statement in ABAP 7.4 – Pretty Printer SAP
Adding/Removing Leading Zero – Pretty Printer SAP
Ok, now let’s start testing the implementation. To test the service implementation, we need to register the service. So that it will expose to the outer world. Below are the steps
Now it will open up the tcode /IWFND/GW_CLIENT
URI for GET/Single read operation would in the format of
/sap/opu/odata/SAP/<Service name>/<Entity Set>(<key name>='<key value>’)
in our case its /sap/opu/odata/SAP/ZXYZ_SAMPLE_ODATA_SRV/OrderrHeaderSet(Vbeln=’18581′)
Note: To debug the code, you set the external break-point in the method. In the case of central hub scenario then there must a Trusted RFC between the frontend and backend system. Based on the RFC configuration external breakpoint can set for RFC user/for testing user. (we will check the scenario of central hub system in due course)
The most common error, we used to get when er_entity that is the response is sent as empty
Also, sometimes you may have to change logic but it is not reflecting or breakpoints are not set for debugging. In that case, you may try to close the transaction & back again or clear the cache
Additional
In the GET_ENTITY method, there is one parameter called io_tech_request_context which holds information about the request and it is a type reference of below interface having following methods
Let’s check an example
DATA(lt_tab) = io_tech_request_context->get_keys( ). "Return key value pair
DATA:ls_entity LIKE er_entity.
io_tech_request_context->get_converted_keys(
IMPORTING
es_key_values = ls_entity "Applied Conversion like leading zero
).
Next Part: Query Operation(GET_ENTITYSET) in OData – Part 5-ABAP Skill
Previous Part:OData Service Creation In SAP – Part 3 – ABAP Skill
2 Comments
Query Operation(GET_ENTITYSET) in OData – Part 5-ABAP Skill · July 14, 2021 at 7:00 am
[…] entity set, there is a method called <entityset name>_GET_ENTITYSET . in the previous part (part 4), we covered the GET_ENTITY method which is returning response as single records(in ABAP word it is […]
OData Service Creation In SAP – Part 3 - ABAP Skill · July 18, 2021 at 5:37 am
[…] Apart from these four classes, there is a technical service name(ending with SRV) and model(MDL) name generated which is used for service registration purposes.We will discuss more details about artifacts in due course. Next Part:Get Operation in OData Service – Part 4 – ABAP Skill […]