How to create OData service for ABAP CDS Views

Hello everyone in this tutorial we will see on how to create SAP OData service for an ABAP CDS views. Lets get started

We usually create OData services in SAP Gateway system using transaction SAP Gateway Service Builder(SEGW). With introduction of ABAP CDS views in SAP Business Suite 4(S/4 HANA), several thousands of CDS views are created for efficient and faster data access. A new and easy way of creating OData services based on CDS views was introduced using the annotation @OData.publish: true at view level.

Lets see how we can expose the ABAP CDS View as OData Service using an example and below is the exposure process.

Step-by-Step Procedure

Example #1 – ABAP CDS View with out Parameters

1. Create an ABAP CDS view using ABAP Development Tools in Eclipse. Copy and paste the below code in DDL editor.

CDS view should meet following rules for successful OData service generation.

No syntax errors in DDL source code.

At least one key element is defined in the SELECT list of the CDS view.

The name of the CDS view should not exceed 26 characters in length.

4. After activating the CDS view, following Gateway artifacts will be generated by the SADL framework in backend server.

Technical service name artifact – <cds_view_name>_CDS.

Gateway Model artifact with name – <cds_view_name>_CDS.

ABAP class with name – CL_<cds_view_name>.

You can find all these artifacts in the transaction code – “/IWBEP/REG_SERVICE”.

5. Launch the transaction code “/IWFND/MAINT_SERVICE” to activate the OData service in hub system. Enter System Alias, Technical Service Name and click the Get Services button. The service will be displayed for selection.

6. In the Add Service dialog box enter the package name. Technical service name and Technical model name will already be suggested. Click on OK to continue.

7. A dialog box appears as shown below, click on OK to continue.

8. As a result OData service is activated successfully in the Gateway hub system. Launch the transaction code “/IWFND/GW_CLIENT” to test the service.

Above is the metadata generated for the OData service created based on the ABAP CDS view. Lets closely look at the metadata and identify the different artifacts as part of the OData service.

Entity Type – ZODATA_DEMOType

Entity Set – ZODATA_DEMO

Now lets see the data by accessing the entity set
http://<server>:8001/sap/opu/odata/sap/ZODATA_DEMO_CDS/ZODATA_DEMO

NOTE: As of now there is no annotation/a way to make the input parameters OPTIONAL in ABAP CDS Views. So you have to pass values for all input parameters in ABAP CDS view while accessing the data from it.

2. Repeat the steps explained above in the Example #1 to create the OData service for this CDS view also.

3. On successfully OData service generation, execute the OData service and look at the metadata generated and following are the 2 entity sets generated

ZODATA_DEMO_PARAMSSet – Entity set which has 2 key predicates, one is the input parameter and the second one is the key of ABAP CDS View. You need to pass these 2 key predicates while accessing the data

ZODATA_DEMO_PARAMS – Entity set which has 1 key predicate, which is input parameter. You need to pass this key predicate to access the data.

To access the data you can use both entity sets and lets look how we can form the URI for both of them.

If you like to use entity set ZODATA_DEMO_PARAMSSet following the URI to be used /sap/opu/odata/sap/ZODATA_DEMO_PARAMS_CDS/ZODATA_DEMO_PARAMSSet(p_carrid=’AA’,AirlineCode=’AA’)

If you like to use entity set ZODATA_DEMO_PARAMS following the URI to be used /sap/opu/odata/sap/ZODATA_DEMO_PARAMS_CDS/ZODATA_DEMO_PARAMS(p_carrid=’AA’)/Set

Congrats! ? You have successfully created an OData service for an ABAP CDS view. Please stay tuned for more tutorials. Leave a comment in the below comment section and let us know your feedback.