
The service model was created by importing a DDIC structure in the SAP Gateway project and the runtime artifacts were generated. The next step is to provide source code to handle OData requests. This can be done in the following ways:
- Implement ABAP-Code
- You redefine and implement the respective methods of the data provider extension class (DPC_EXT) in the ABAP Workbench or ABAP Development Tools.
- Code Mapping
- You establish a relationship between the interface parameters of a reuse unit and the properties of an entity set using the built-in mapping tool of the SAP Gateway Service Builder.
We skip the code mapping for now and focus on implementing the ABAP-code.

Implementing the methods in the DPC_EXT means overwriting the corresponding methods from the DPC. The most frequently redefined methods are as follows:
- <Entity Set>_CREATE_ENTITY – creates the resource corresponding to a specified entity type
- <Entity Set>_DELETE_ENTITY – deletes a resource of a specified entity type
- <Entity Set>_GET_ENTITY – retrieves an entity of a specified entity type
- <Entity Set>_GET_ENTITYSET – retrieves a collection of a specified entity type
- <Entity Set>_UPDATE_ENTITY – changes a resource for a specified entity type
These methods are listed in the SAP Gateway project under Service Implementation for each entity set. The context menu entry Go to ABAP Implementation forwards you directly in the selected method implementation.

If the method is not yet implemented in the DPC_EXT, an information popup informs you about that and the Continue button opens the DPC_EXT itself in the ABAP Workbench. There you can redefine the method as usual.
In our example, we want to implement the query operation for all products. The PRODUCTSET_GET_ENTITYSET method can be opened following the path Service Implementation → ProductSet → GetEntitySet (Query) → Got to ABAP Workbench. It is not yet implemented. So we select it in the navigation area and choose Redefine.
Caution

For an <Entity Set>_GET_ENTITYSET method, the export parameter et_entityset should be filled with the data requested by the OData request. The parameter is typed using a tt_<Entity Type> table type generated in the MPC based on the definition of the entity type in the data model.
It is recommended to call a reuse unit for reading the requested data instead of implementing any SQL statement in this method. In that way, there is a clear separation of service implementation and database operation. In this example, the BAPI_EPM_PRODUCT_GET_LIST is called to get all products from the database.
Errors should be raised by using the exception classes /IWBEP/CX_MGW_BUSI_EXCEPTION for errors in the business logic, while technical exceptions should be raised via /IWBEP/CX_MGW_TECH_EXCEPTION. For example, checks should be implemented if an entity set is marked as non-addressable.
The exception classes offer several parameters to provide more details about the error. For example, the message_container parameter allows to collect multiple messages in one object. The mo_context attribute of the DPC provides such a message container, which can be filled using several methods like add_message_from_bapi() awaiting the return parameter of a BAPI.
Finally, CORRESPONDING or MOVE-CORRESPONDING is used to pass the result to the SAP Gateway framework. This is useful if not all fields of the structure provided by the reuse unit have been used for the entity set. In addition, the implementation would also work if the underlying structure is enhanced by an append structure.
The following block provides the example source code in a copy-friendly way:
1234567891011121314151617181920212223METHOD productset_get_entityset.
DATA: lt_headerdata TYPE TABLE OF bapi_epm_product_header,
lt_return TYPE TABLE OF bapiret2.
* Get data
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
TABLES
headerdata = lt_headerdata
return = lt_return.
IF lt_return IS NOT INITIAL.
" Message Container
mo_context->get_message_container( )
->add_messages_from_bapi( lt_return ).
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message_container = mo_context->get_message_container( ).
ENDIF.
* Fill response data
et_entityset = CORRESPONDING #( lt_headerdata ).
ENDMETHOD.
