SAP Gateway SEGW DPC_EXT CRUD Methods Generator for OData in ABAP
This generator writes the five DPC_EXT method redefinitions behind a classic SEGW OData V2 service: entity set read with $filter and paging, single read, create, update and delete. Fill in your project, entity set, structure type and backend table and paste the result into your DPC_EXT class.
Fill in
Method names are <entity set>_GET_ENTITYSET and so on; SEGW shortens long set names to fit 30 characters, so copy the exact name from the DPC class.
1. Class definition: redefine the methods
" Project ZPM_ORDERS, entity type Order, entity set OrderSet
" In SE24 or SEGW use Redefine; in ADT add the lines below.
CLASS ZCL_ZPM_ORDERS_DPC_EXT DEFINITION
PUBLIC
INHERITING FROM ZCL_ZPM_ORDERS_DPC
CREATE PUBLIC.
PROTECTED SECTION.
METHODS OrderSet_get_entityset REDEFINITION.
METHODS OrderSet_get_entity REDEFINITION.
METHODS OrderSet_create_entity REDEFINITION.
METHODS OrderSet_update_entity REDEFINITION.
METHODS OrderSet_delete_entity REDEFINITION.
ENDCLASS.2. GET_ENTITYSET ($filter, $top, $skip, $inlinecount)
METHOD OrderSet_get_entityset.
DATA lr_key TYPE RANGE OF AUFK-AUFNR.
DATA lt_entityset LIKE et_entityset.
DATA lv_max TYPE i.
" Filter on the key: technical context returns ABAP (internal) property names
DATA(lt_filter) = io_tech_request_context->get_filter( )->get_filter_select_options( ).
READ TABLE lt_filter INTO DATA(ls_filter) WITH KEY property = 'AUFNR'.
IF sy-subrc = 0.
lr_key = CORRESPONDING #( ls_filter-select_options ).
ENDIF.
" Read only what the page needs: $top plus $skip rows (0 means no limit)
IF is_paging-top > 0.
lv_max = is_paging-top + is_paging-skip.
ENDIF.
SELECT aufnr, auart, ktext, werks, erdat
FROM AUFK
WHERE AUFNR IN @lr_key
ORDER BY AUFNR
INTO CORRESPONDING FIELDS OF TABLE @lt_entityset
UP TO @lv_max ROWS.
IF is_paging-skip > 0.
DELETE lt_entityset FROM 1 TO is_paging-skip.
ENDIF.
IF io_tech_request_context->has_inlinecount( ) = abap_true.
SELECT COUNT(*) FROM AUFK
WHERE AUFNR IN @lr_key
INTO @DATA(lv_count).
es_response_context-inlinecount = lv_count.
ENDIF.
et_entityset = lt_entityset.
ENDMETHOD.3. GET_ENTITY (read one by key)
METHOD OrderSet_get_entity.
DATA lv_key TYPE AUFK-AUFNR.
READ TABLE it_key_tab INTO DATA(ls_key) WITH KEY name = 'Aufnr'.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'Key Aufnr missing in request'.
ENDIF.
" Add leading zeros, for example order 4711 becomes 000000004711
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = ls_key-value
IMPORTING
output = lv_key.
SELECT SINGLE aufnr, auart, ktext, werks, erdat
FROM AUFK
WHERE AUFNR = @lv_key
INTO CORRESPONDING FIELDS OF @er_entity.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'Entry not found'.
ENDIF.
ENDMETHOD.4. CREATE_ENTITY (HTTP POST)
METHOD OrderSet_create_entity.
DATA ls_entity TYPE ZCL_ZPM_ORDERS_MPC=>TS_ORDER.
io_data_provider->read_entry_data( IMPORTING es_data = ls_entity ).
IF ls_entity-ktext IS INITIAL.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'ktext is required'.
ENDIF.
" Create the business object with a released BAPI here and map
" its RETURN messages to the exception above. Never INSERT into
" standard tables such as AUFK directly.
er_entity = ls_entity.
ENDMETHOD.5. UPDATE_ENTITY (HTTP PUT)
METHOD OrderSet_update_entity.
DATA ls_entity TYPE ZCL_ZPM_ORDERS_MPC=>TS_ORDER.
READ TABLE it_key_tab INTO DATA(ls_key) WITH KEY name = 'Aufnr'.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'Key Aufnr missing in request'.
ENDIF.
io_data_provider->read_entry_data( IMPORTING es_data = ls_entity ).
" The key in the URL wins over any key in the payload
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = ls_key-value
IMPORTING
output = ls_entity-AUFNR.
" Change the business object with a released BAPI here.
er_entity = ls_entity.
ENDMETHOD.6. DELETE_ENTITY (HTTP DELETE)
METHOD OrderSet_delete_entity.
DATA lv_key TYPE AUFK-AUFNR.
READ TABLE it_key_tab INTO DATA(ls_key) WITH KEY name = 'Aufnr'.
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'Key Aufnr missing in request'.
ENDIF.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = ls_key-value
IMPORTING
output = lv_key.
SELECT SINGLE @abap_true FROM AUFK
WHERE AUFNR = @lv_key
INTO @DATA(lv_exists).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = 'Entry not found'.
ENDIF.
" Set the deletion flag or status with a released BAPI here.
ENDMETHOD.7. Test requests in /IWFND/GW_CLIENT
GET /sap/opu/odata/sap/ZPM_ORDERS_SRV/OrderSet?$filter=Aufnr eq '4711'&$top=20&$skip=0&$inlinecount=allpages
GET /sap/opu/odata/sap/ZPM_ORDERS_SRV/OrderSet('000000004711')
For POST, PUT and DELETE first send a GET with header
X-CSRF-Token: Fetch
then copy the returned token into the X-CSRF-Token header of the write request.Written for SAP Gateway OData V2 (SEGW) on ABAP 7.50 or later (new Open SQL with @ host variables and inline declarations). Check in your DPC base class that the method names match the generated ones, that the entity structure components are named like the table fields you select, and that the service name follows your registration (default technical name is the project name plus _SRV). Use released BAPIs for writes.
Get your Gateway service reviewed
Send us your SEGW model and DPC_EXT code and we will point out paging, key conversion and error handling issues before go-live.
Optional. The tool and every export work without this. No spam.
How to use it: OData in SAP ABAP, step by step
Create the project in SEGW, define the entity type and entity set (or import them from a DDIC structure), then Generate Runtime Objects. SEGW creates the MPC and DPC base classes plus the _EXT classes you own. Fill in the fields here with the exact names from that generation, paste block 1 into the DPC_EXT definition and blocks 2 to 6 into the implementation, and activate. Register the service on the hub in /IWFND/MAINT_SERVICE (Add Service, system alias, technical service name), then test it in /IWFND/GW_CLIENT using the requests in block 7.
- Regenerate runtime objects after every model change; the _EXT classes keep your code.
- Delete the method blocks you do not need, and their REDEFINITION lines.
- Keep the technical names: the URL uses the external property name, the technical request context uses the ABAP name.
What each method receives
All five methods come from /IWBEP/IF_MGW_APPL_SRV_RUNTIME through the generated DPC class. GET_ENTITYSET gets IS_PAGING (TOP and SKIP from $top and $skip), IO_TECH_REQUEST_CONTEXT (filter, order, inlinecount) and returns ET_ENTITYSET and ES_RESPONSE_CONTEXT, where INLINECOUNT carries the total for $inlinecount=allpages. GET_ENTITY, UPDATE_ENTITY and DELETE_ENTITY get IT_KEY_TAB, a name/value table with external property names such as Aufnr. CREATE_ENTITY and UPDATE_ENTITY get IO_DATA_PROVIDER, whose READ_ENTRY_DATA fills your structure from the JSON or XML payload, and return ER_ENTITY. Errors go back to the client by raising /IWBEP/CX_MGW_BUSI_EXCEPTION with TEXTID business_error and a MESSAGE text.
Worked example: maintenance order service on AUFK
With the defaults, a request for OrderSet?$filter=Aufnr eq '4711'&$top=20 reads AUFK through a range built from the filter, sorted by AUFNR, limited to 20 rows plus any $skip, and returns AUFNR, AUART, KTEXT, WERKS and ERDAT. OrderSet('000000004711') hits GET_ENTITY; the key is passed through CONVERSION_EXIT_ALPHA_INPUT so that a client sending 4711 still finds the order. A POST with a JSON body is mapped into TS_ORDER and rejected with a business error if KTEXT is empty. The write blocks deliberately leave the database change to a released BAPI, because writing AUFK directly skips status management, change documents and authority checks.
Gotchas and where to look when it fails
A 403 on POST almost always means a missing or stale CSRF token: fetch it with a GET first in the same session. A 500 with no detail is logged on the hub in /IWFND/ERROR_LOG and on the backend in /IWBEP/ERROR_LOG, both with the call stack. If the filter is ignored, check that you read the property by its ABAP name from IO_TECH_REQUEST_CONTEXT, or by the external name if you use IT_FILTER_SELECT_OPTIONS instead. Paging without ORDER BY returns unstable pages. Inside $batch changesets, Gateway rejects COMMIT WORK in the CRUD methods unless you implement deferred processing in CHANGESET_BEGIN and CHANGESET_END.
Frequently Asked Questions
What are the steps to create an OData service in SAP?
In SEGW create a project, model the entity type and entity set, generate runtime objects, redefine the CRUD methods in the DPC_EXT class, activate, then register the service on the Gateway hub in /IWFND/MAINT_SERVICE with the right system alias. Test with /IWFND/GW_CLIENT and check /IWFND/ERROR_LOG if anything fails.
How do I write an SAP OData POST example in ABAP?
Redefine <entity set>_CREATE_ENTITY, call io_data_provider->read_entry_data( IMPORTING es_data = ls_entity ), validate, create the object with a BAPI and return the result in ER_ENTITY. Clients must send a CSRF token fetched with a prior GET, otherwise the POST fails with HTTP 403.
Why does $inlinecount return zero?
Gateway does not count for you in a custom GET_ENTITYSET. Check io_tech_request_context->has_inlinecount( ) and set es_response_context-inlinecount to the total number of matching rows, counted before paging. The generator does this with a separate SELECT COUNT(*) on the same WHERE clause.
Should I use SEGW or RAP for a new OData service?
On S/4HANA and recent ABAP platforms SAP recommends RAP with CDS views for new services, including OData V4. SEGW remains common on ECC, older NetWeaver Gateway hubs and for extending existing services. This generator is for the SEGW DPC_EXT model only.
If you want people to ask questions of your SAP order data in plain English on your own servers, using services like this one as read-only tools, we can set that up.
Related Tools
SAP OData Service URL Builder
Generate a correct SAP Gateway OData V2 request URL with query options, test it in a browser or REST client, then reuse it in your code.
SAPSAP OData V2 and V4 Query Options Reference
A lookup table of OData system query options, filter operators and functions, showing which work in V2, which in V4, and an example for each.
Developer ToolsSAP ABAP AUTHORITY-CHECK Snippet Generator
Fill in an authorization object, activity and organisational field and get a ready ABAP AUTHORITY-CHECK block with error handling.
Developer ToolsSAP BW Process Chain API ABAP Generator (RSPC_API_CHAIN_START)
Generate an ABAP report that starts a process chain through the RSPC API, polls its status until green or red, interrupts a schedule, or lists scheduled chains.
More Developer Tools tools
See all- ABAP New Open SQL Syntax Reference by Release (7.40 to 7.58)
- ABAP ALV Layout, Field Catalog and Color Codes Reference
- SAP GUI Scripting Script Generator (Python win32com)
- ABAP Selection Screen and SELECT-OPTIONS Generator
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
Keep exploring
Free tools
- SAP Gateway OData Transaction Codes Reference (SEGW, /IWFND/)Every Gateway transaction you need to build, activate, test and debug OData services, with where it runs (hub or backend) and the mistake people make with it.
- SAP Gateway OData Service Activation and Transport ChecklistStep by step checks to activate, test and transport an OData service on SAP Gateway, embedded or hub, V2 or V4.
- SAP NetWeaver Release to SAP_BASIS and Kernel ReferenceWhich SAP_BASIS release belongs to each NetWeaver and ABAP Platform release, which products run on it, which kernel lines apply and how to check your own system.
ERP fixes and how-tos
- Lookup transformation in BODS: lookup_ext explained with examplesHow lookup and lookup_ext work in SAP BusinessObjects Data Services, with cache options, a mapping example and how to load data in a migration job.
- API Creation Process in SAP S/4HANA: From CDS View to OData ServiceHow to create a custom API in SAP S/4HANA: CDS view entity, service definition, service binding, activation, authorization and when to use the Business Hub.