In SAP terms, Odata a platform/framework that can be used to create SAP objects or services that can be consumed from outside of SAP box to read or write data. A service can be called an API that SAP provides to their consumers.
Any consumer or provider of SAP who can call REST APIs can use it. It’s OOP based, faster, secure and having nice trace tools.
V4 is latest version of OData and its for data reduction. Size of Meta data can be controlled in service output, enhanced paging technique. V4 is different from SEGW project creation to DPC Ext methods, coding, service registration, URI, Json format, debugging etc.
Creation of a V4 service through SEGW:
Odata V4 came with Code based implementation initially, with NW 750 it can be created through SEGW.
Go to Tcode SEGW (SAP Gateway service builder).
Create a new project, select Odata V4 for project type-
V4 V2
We don’t have Associations same like in V2, but that can be done in a different way.
Create entity types (data structures) and define their properties:
For multiple and deep structures like header and item or item and serial numbers, define their Navigation properties:
Here navigation property of header ‘Items’ have target type entity ‘Item’ and Collection is checked means a header can have multiple items.
Same for item to serial:
Define Entity Sets and create bindings with Navigation properties created above:
Activate the project, provide MPC DPC Extension class name:
Service ZAPI_GET_PLANTSLOC gets created.
Register and publish the service:
There are 2 step process to register the service. If we have a separate SAP back-end and gateway system like SAP ECC and Fiori then we’ll register the service in SAP ECC and publish the same in SAP Fiori. If SAP gateway is in ECC, we do both steps in ECC.
Step 1 – Register the service
Go to tocde /iwbep/v4_admin (SAP back-end Service Administration) in SAP ECC:
Create a service group – We can define a Service group by similar business area or similar process area. Example – ZDIS_TRANSFER_POSTING if for all transfer postings (MIGO) in distribution area.
Provide service name and MPD DPC EXT class names:
By default this service bind in default service group, move it to the group we have created:
Service is registered:
Step 2 – Publish the service group
Go to tocde /iwfnd/v4_admin in (SAP Gateway Service Administration) in SAP Fiori or SAP gateway system:
Through Routing configuration we can setup system Alias with RFC destination(Connection between SAP back-end and gateway system.
Click on Publish Service Group. Provide System Alias (Alias created for ECC->Fiori system) and Service group name, hit Get service groups:
Service and group will be successfully imported in Gateway system. Publish the group, lock the configuration in transport request.
Click on Metadata to display the metadata information or Service test to test the service from SAP Gateway (/IWFND/GW_CLIENT):
Metadata:
Sometimes if we get below error:
Go to Tcode /IWFND/VIRUS_SCAN to omit V4 Virus scan and execute after selecting below checkbox:
Adding code in DPC Extension:
Below are the methods we get in DPC EXT class:
Available Interfaces in V4:
Name |
Details |
/IWBEP/IF_V4_DP_BASIC |
Methods provide basic functionality
(Create, Update, Delete, Navigation) |
/IWBEP/IF_V4_DP_INTERMEDIATE |
Medium complex functionality
eTag handling, PATCH, $expand
Contains generic calls to other (especially the basic) interfaces |
/IWBEP/IF_V4_DP_ADVANCED |
Always called first by the framework
Contains generic calls to the other (especially the basic) interfaces |
/IWBEP/IF_V4_DP_BATCH |
$batch. Generic $batch and change-set |
/IWBEP/IF_V4_DP_PROCESS_STEPS |
Transaction and life cycle handling |
According to service entity structures, Odata framework make a call to corresponding method of Advanced Interface then from this, call come to Basic interface method.
Example – for read a list call comes to /IWBEP/IF_V4_DP_ADVANCED~READ_ENTITY_LIST method and if there is no custom re-definition found, code of this method calls to /IWBEP/IF_V4_DP_BASIC~READ_ENTITY_LIST method. As per our need we can implement the method.
Import parameters
Each interface method has 2 importing parameters:
IO_REQUEST: to get information we need to handle the request
IO_RESPONSE: to return business data to the SAP Gateway framework and to tell the framework which processing steps the service implementation has handled itself
ToDo, Done list
‘Processing steps the service implementation has handled itself’
This is handled by ToDo and Done flags.
To do flags tells us what the implementation needs to do.
Example – Any get call with filterable values ‘Filter’ flag will have ‘X’.
In a Post call, todo list flags can be different. Basically it depends on type of Interface method we redefine.
At the end of processing set Done list flags to inform Odata framework what the implementation did.
Common methods to use
Some of the common and useful methods:
IO_REQUEST:
- GET_ENTITY_SET – Read entity set name to validate and control the flow
- GET_FILTER_OSQL_WHERE_CLAUSE – Read where clause (mainly used in CDS views)
- GET_FILTER_PROPS_WITH_RANGES – Provide filter fields passed in a call
- GET_FILTER_RANGES_FOR_PROP – Read filter values
- GET_KEY_DATA – Read key data if passed (can be used if 1-2 filterable fields in a get call)
- GET_TODOS – Get To do flags
- GET_SKIP - Get Skip value (($skip) for paging
- GET_TOP – Get Top value ($top) for paging
- GET_BUSI_DATA – Read Input Json in Post or create call
IO_RESPONSE:
- SET_BUSI_DATA - Set the business data. Update service output
- SET_IS_DONE – Set Done flags
- SET_COUNT – In case need to send total number of records
- GET_MESSAGE_CONTAINER - Message Container for messages. To collect error messages to pass in Exception object. This will return an Object reference to collect the messages
- ADD_RUNTIME_STATISTICS – In get call, send Application Time
Code lines:
io_request->get_entity_set(
IMPORTING
ev_entity_set_name = DATA(lv_entity_set_name) ).
io_request->get_todos( IMPORTING es_todo_list = lst_todo_list ).
io_request->get_busi_data(
IMPORTING
es_busi_data = li_busidata ). “li_busidata is type of entity structure
------Process on li_busidata-------
lst_done_list-busi_data = abap_true.
lst_done_list-deep_busi_data = abap_true. “If deep entity exists (Get to do list can be checked)
io_response->set_busi_data(
EXPORTING
is_busi_data = li_busidata ). “Setting service O/P
io_response->set_is_done( lst_done_list ). “Setting Done list flags
lo_response_fnl ?= io_response.
lo_response_fnl TYPE REF TO /iwbep/cl_v4_response_info_pro.
lo_response_fnl->finalize( ). “Can be used in Post call to inform V4 Response framework
Exception handling
Exception - /IWBEP/CX_GATEWAY
Exception class: /IWBEP/CX_GATEWAY
Code lines:
DATA(lo_message) = im_response->get_message_container( ).
lo_message->add_t100(
EXPORTING
iv_msg_type = <lfs_return>-type " Message Type
iv_msg_id = <lfs_return>-id " Message Class
iv_msg_number = <lfs_return>-number ). " Message Number
DATA(lo_exp) = NEW /iwbep/cx_gateway(
http_status_code = '404' “Pass HTTP error code according to the situation
message_container = lo_message ).
RAISE EXCEPTION lo_exp.
Available HTTP Error codes (Public section of exception class /IWBEP/CX_GATEWAY):
HTTP Code |
Description |
304 |
Not Modified - the data is up to date already and does not need to be sent |
400 |
Bad Request - The request cannot be fulfilled due to bad syntax. - Should not happen in the BEP |
403 |
Forbidden - The request was a legal request, but the server is refusing to respond to it |
404 |
Not Found - The requested resource could not be found but may be available again in the future |
405 |
Method Not Allowed - A request was made of a resource using a request method not supported by that resource |
406 |
Not Acceptable - The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request |
409 |
Conflict - Indicates that the request could not be processed because of conflict in the request, such as an edit conflict |
410 |
Gone - Indicates that a resources existed earlier but it’s not available anymore |
412 |
Precondition Failed - The server does not meet one of the preconditions that the requester put on the request |
415 |
Unsupported Media Type - The request entity has a media type which the server or resource does not support |
428 |
Precondition Required (RFC 6585) - The origin server requires the request to be conditional |
500 |
Server: Internal Server Error - A generic error message, given when no more specific message is suitable |
501 |
Server: Not Implemented - The server either does not recognize the request method, or it lacks the ability to fulfill the request |
503 |
Server: Service Unavailable - The server is currently unavailable (because it is overloaded or down for maintenance). Temporary error |
Testing V4 from SAP Gateway:
Changes in URI:
V2: /sap/opu/odata/SAP/zdis_get_po/POHeaderS?$expand=POHeaderToPOItem,POHeaderToReturnMsg&$filter=PoNumber eq '3000000477'&$format=json
V4: /sap/opu/odata4/sap/zdis_get_master_data/default/sap/zapi_get_plantsloc/0001/PlantStorageLocationDetailsSet?$filter=(Plant eq '1000' or Plant eq '2000') and CompanyCode eq 'CC01'
In V4 we need to pass service group along with service name.
V4 Json sample (Post):
{
"PostingDate" : "2019-10-02", “Date format for type Edm.Date
"UserId" : "DUMMY",
"OrderNumber" : "1234567890",
"Message" : "",
"Items" : [
{
"ItemNumber" : "0001",
"ProjectNumber" : "F.01.000289",
"Plant" : "1000",
"StogareLocation" : "1100",
"Material" : "MM18",
"Quantity" : 1,
"MovementType" : "101",
"SpecialStockIndicator" : "Q",
"Batch" : "",
"ValuationType" : "",
"UnitOfMeasure" : "KG",
"Serials" : [
{
"SerialNumber" : ""
}
]
},
{
"ItemNumber" : "0002",
"ProjectNumber" : "F.01.000289",
"Plant" : "1000",
"StogareLocation" : "1100",
"Material" : "MM19",
"Quantity" : 1,
"MovementType" : "301",
"SpecialStockIndicator" : "",
"Batch" : "",
"ValuationType" : "",
"UnitOfMeasure" : "LB",
"Serials" : [
{
"SerialNumber" : ""
}
]
}
]
}
Debugging V4:
Below method calls SAP backend system from SAP Gateway system. Put one break point here on call of backend RFC and one another on DPC EXT method that we have implemented.
V2: /IWFND/CL_MGW_REQUEST_MANAGER~ CALL_BEP
V4: /IWBEP/CL_V4_REMOTE_PROXY~CALL_BEP
RFC FM in ECC: /IWBEP/FM_V4_HANDLE_REQUEST
Search V4 services:
Search existing V4s in your system using below tables–
/IWBEP/I_V4_MSRV - OData V4 Service Registry
/IWBEP/I_V4_MSGA - OData V4 Service Group Assignment
Conclusion:
Odata URI can be called from consumer’s app or using tools like Postman. As it’s not recommended to expose SAP system address and service information out, we can use cloud based API proxy technique to add additional security. It uses industries best authentication methods like OAuth2 or SMAL SSO, provide a platform for tracing incoming or outgoing traffic, provide details of service performance and work as a middle ware where we can do lookup and error handling.