Using a BAPI for booking a Flight in SAP’s Flight Data Model

A Business Application Programming Interface (BAPI) is a precisely defined interface providing access to processes and data in business application systems such as R/3. Implemented as Remote Function Calls (RFC) they allow both SAP components and non-SAP components, to access and handle SAP business objects. The following tutorial shows how to use a BAPI for an OData service:

Following the tutorial will create an implementation for “SBOOKSET_CREATE_ENTITY”. When using the BAPI, it is important, that the data that is passed to the OData service and subsequently to the BAPI is correct, otherwise the BAPI will throw an exception. The data is passed via “submitChanges()” in the Fiori frontend.

Deprecated Gateway APIs

As Zamani explains, some of the Gateway APIs for implementing OData services are deprecated. The only reason that they are still available is to avoid syntax errors when customers upgrade. Instead, the replacement APIs should be used. One of the examples for a replacement API is the importing parameter “IO_TECH_REQUEST_CONTEXT” for accessing the request data in a service.

https://blogs.sap.com/2017/02/01/avoid-using-deprecated-sap-gateway-apis-in-your-odata-service-implementation

Booking a flight with SAPBC_GLOBAL_BOOK

In order to use “SAPBC_GLOBAL_BOOK” for booking a flight, the method “SBOOKSET_GET_ENTITY” has to be altered. Because the “IT_KEY_TAB”-property is deprecated, it cannot be used in order to obtain the database keys (connid, carrid, fldate, bookid). A possible remedy is to access the “IO_TECH_REQUEST_CONTEXT”-object instead. By calling its “get_keys( )” method, the relevant keys can be obtained.

method SBOOKSET_GET_ENTITY.
DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs.
lt_keys = io_tech_request_context->get_keys( ).
    FIELD-SYMBOLS <key_fldate> TYPE /iwbep/s_mgw_tech_pair.
FIELD-SYMBOLS <key_connid> TYPE /iwbep/s_mgw_tech_pair.
FIELD-SYMBOLS <key_carrid> TYPE /iwbep/s_mgw_tech_pair.
FIELD-SYMBOLS <key_bookid> TYPE /iwbep/s_mgw_tech_pair.
* WICHTIG: die Schluessel haben die Reihenfolge wie in der Entitaet    READ TABLE lt_keys ASSIGNING <key_carrid> INDEX 1.
READ TABLE lt_keys ASSIGNING <key_connid> INDEX 2.
READ TABLE lt_keys ASSIGNING <key_fldate> INDEX 3.
READ TABLE lt_keys ASSIGNING <key_bookid> INDEX 4.
    SELECT SINGLE * FROM sbook INTO er_entity WHERE
carrid LIKE <key_carrid>-value
AND connid LIKE <key_connid>-value
AND fldate LIKE <key_fldate>-value
AND bookid LIKE <key_bookid>-value.
endmethod.

The relevant keys are obtained and stored into lo_tech_request_context within the last section of “SBOOKSET_CREATE_ENTITY”.

*------------------------------------------------------------------*
* - Read After Create -
*------------------------------------------------------------------*
CREATE OBJECT lo_tech_read_request_context.
* Create key table for the read operation
ls_key-name = 'CARRID'.
ls_key-value = iv_carrid.
IF ls_key-value IS NOT INITIAL.
APPEND ls_key TO lt_keys.
ENDIF.
ls_key-name = 'CONNID'.
ls_key-value = iv_connid.
IF ls_key-value IS NOT INITIAL.
APPEND ls_key TO lt_keys.
ENDIF.
ls_key-name = 'FLDATE'.
ls_key-value = iv_fldate.
IF ls_key-value IS NOT INITIAL.
APPEND ls_key TO lt_keys.
ENDIF.
ls_key-name = 'BOOKID'.
ls_key-value = ev_bookid.
IF ls_key-value IS NOT INITIAL.
APPEND ls_key TO lt_keys.
ENDIF.
* Set into request context object the key table and the entity set name
lo_tech_read_request_context->set_keys( IMPORTING et_keys = lt_keys ).
lv_entityset_name = io_tech_request_context->get_entity_set_name( ).
lo_tech_read_request_context->set_entityset_name( IMPORTING ev_entityset_name = lv_entityset_name ).
* Call read after create
/iwbep/if_mgw_appl_srv_runtime~get_entity(
EXPORTING
iv_entity_name = iv_entity_name
iv_entity_set_name = iv_entity_set_name
iv_source_name = iv_source_name
it_key_tab = it_key_tab
io_tech_request_context = lo_tech_read_request_context
it_navigation_path = it_navigation_path
IMPORTING
er_entity = ls_entity ).
* Send the read response to the caller interface
ASSIGN ls_entity->* TO <ls_data>.
er_entity = <ls_data>.
ENDMETHOD.

This blog further elaborates on “IO_TECH_REQUEST_CONTEXT”: