Extend a Custom Core Data Service in ABAP Environment
- How to extend SAP or custom table using Extensibility model
- How to extend a CDS view by adding a new field
- How to implement custom logic
- How to consume fields from other exposed CDS view using Associations
Prerequisites
- You need to have access to an SAP BTP, ABAP environment, or SAP S/4HANA Cloud, ABAP environment or SAP S/4HANA (release 2022 or higher) system.
For example, you can create a free trial user on SAP BTP, ABAP environment. - You have downloaded and installed the latest ABAP Development Tools (ADT) on the latest Eclipse© platform.
- You have created an ABAP Cloud Project.
- Your system has the ABAP flight reference scenario. If your system hasn’t this scenario. You can download it here. The trial systems have the flight scenario included.
This tutorial was written for SAP BTP ABAP Environment. However, you should also be able to use it in SAP S/4HANA Cloud Environment in the same way.
Always replace ### with your initials or group number.
- Step 1
-
Open Eclipse and connect to your system.
-
Right-click on the package
ZLOCALand choose New > ABAP Package from the context menu. -
Create your own ABAP development package
Z_Travel_###as a sub package ofZLOCAL.- Name:
Z_Travel_### - Description:
My travel package - Select the Add to favorite packages box
- Name:
-
Click Next.

-
Select package properties and click Next.
- Software Component:
ZLOCAL

- Software Component:
-
Select a transport request or create new and click Finish.

-
- Step 2
Create a Structure with a field
Description.-
Right-click on your ABAP package
Z_Travel_###and select New > Other ABAP Repository Object from the context menu.
-
Enter the filter text Structure > Structure, then choose Next.
-
Enter a name such as
ZTravel_struc_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopy@EndUserText.label : 'Structure of Travel Data' @AbapCatalog.enhancement.category : #EXTENSIBLE_ANY @AbapCatalog.enhancement.fieldSuffix : 'ZAC' @AbapCatalog.enhancement.quotaMaximumFields : 350 @AbapCatalog.enhancement.quotaMaximumBytes : 3500 define structure ztravel_struc_### { description : /dmo/description; } -
Save
and activate
the changes.
-
- Step 3
Create a database table to store the Travel data. This Travel table contains
Travel ID,Total PriceandCurrency Code.-
Right-click on your ABAP package
Z_Travel_###and select New > Other ABAP Repository Object from the context menu.
-
Enter the filter text Table > Database table, then choose Next.
-
Enter a name such as
ZTravel_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopy@EndUserText.label : 'Travel data for Extensibility' @AbapCatalog.enhancement.category : #EXTENSIBLE_ANY @AbapCatalog.tableCategory : #TRANSPARENT @AbapCatalog.deliveryClass : #A @AbapCatalog.dataMaintenance : #RESTRICTED define table ztravel_### { key client : abap.clnt not null; key travel_id : /dmo/travel_id not null; @Semantics.amount.currencyCode : 'ztravel_###.currency_code' total_price : /dmo/total_price; currency_code : /dmo/currency_code; include ztravel_struc_###; } -
Save
and activate
the changes.
-
- Step 4
Create an ABAP class to generate demo travel data.
-
Right-click on your ABAP package
Z_Travel_###and select New > ABAP Class from the context menu. -
Enter a name such as
ztravel_fill_data_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopyCLASS ztravel_fill_data_### DEFINITION PUBLIC FINAL CREATE PUBLIC . PUBLIC SECTION. INTERFACES if_oo_adt_classrun. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS ztravel_fill_data_### IMPLEMENTATION. METHOD if_oo_adt_classrun~main. * clear data DELETE FROM ztravel_###. "insert travel demo data INSERT ztravel_### FROM ( SELECT FROM /dmo/travel AS travel FIELDS travel~travel_id AS travel_id, travel~total_price AS total_price, travel~currency_code AS currency_code, travel~description AS description ). COMMIT WORK. out->write( | Data generated for table ztravel_### | ). ENDMETHOD. ENDCLASS. - Save and activate
the changes.
-
Run your console application. For that, select your ABAP class
ztravel_fill_data_###, select the run button > Run As > ABAP Application (Console) F9 or press F9.
-
- Step 5
Create a CDS view
ZITravel_###based on the database tableZTravel_###-
Right-click on your ABAP package
Z_Travel_###and select New > Data Definition from the context menu.
-
Enter a Name such as
ZITravel_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopy@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST] @AccessControl.authorizationCheck: #NOT_REQUIRED @EndUserText.label: 'Travel View Entity' @Metadata.ignorePropagatedAnnotations: true @AbapCatalog.extensibility: { extensible: true, elementSuffix: 'ZAC', quota: { maximumFields: 500, maximumBytes: 5000 }, dataSources: [ '_Travel' ] } define view entity ZITRAVEL_### as select from ztravel_### as _Travel { key travel_id as TravelId, description as Description, @Semantics.amount.currencyCode: 'CurrencyCode' total_price as TotalPrice, currency_code as CurrencyCode } - Save and activate
the CDS view.
-
Click anywhere in the editor and choose Open With > Data Preview from the context menu.

-
- Step 6
Let us consider a database table that is defined by a software provider, for example, SAP. A customer or partner wants to add fields to this database table.
Instead of adding the fields to the database table definition in the dictionary, they create an append structure. The append structure is a separate dictionary object, owned and maintained by the customer or partner.
-
Right-click on the structure
ZTravel_struc_###and select New > Append Structure from the context menu.
-
Enter a Name such as
ZTravel_struc_ext_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and choose Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopy@EndUserText.label : 'Structure for Travel table extension' @AbapCatalog.enhancement.category : #NOT_EXTENSIBLE extend type ztravel_struc_### with ztravel_struc_ext_### { zztraveltype_zac : /dmo/carrier_name; } -
Save
and activate
.
-
- Step 7
The new field
zztraveltype_zacshould be filled with the below logic in the ABAP class.If `total_price` > 4500, set Travel Type to 'Business'. Else, if `total_price` > 3000, set Travel Type to 'Premium Economy'. Otherwise, set Travel Type to 'Economy'-
Right-click on your ABAP package
Z_Travel_###and select New > ABAP Class from the context menu. -
Enter a Name such as
ztravel_ext_data_###- always replacing###with your initials or group number - and a description, then choose Next. -
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopyCLASS ztravel_ext_data_### DEFINITION PUBLIC FINAL CREATE PUBLIC . PUBLIC SECTION. INTERFACES if_oo_adt_classrun. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS ztravel_ext_data_### IMPLEMENTATION. METHOD if_oo_adt_classrun~main. DATA: lt_travel TYPE TABLE OF ztravel_###, ls_travel TYPE ztravel_###. " Select existing data from the table SELECT * FROM ztravel_### INTO TABLE @lt_travel. " Loop through the data and update the new field based on the logic LOOP AT lt_travel INTO ls_travel. IF ls_travel-total_price > 4500. ls_travel-zztraveltype_zac = 'Business'. ELSEIF ls_travel-total_price > 3000 AND ls_travel-total_price < 4500. ls_travel-zzTravelType_zac = 'Premium Economy'. ELSE. ls_travel-zzTravelType_zac = 'Economy'. ENDIF. " Update the table with the new value MODIFY ztravel_### FROM @ls_travel. ENDLOOP. out->write( |Table updated| ). ENDMETHOD. ENDCLASS. - Save and activate
the changes.
-
Run your console application. For that, select your ABAP class
ztravel_ext_data_###, select the run button > Run As > ABAP Application (Console) F9 or press F9.
-
- Step 8
-
Right-click on the CDS View
ZITravel_###and select New > Data Definition from the context menu. -
Enter a Name such as
ZITravel_Ext_###- always replacing###with your initials or group number - and a description, then choose Next.
-
Accept the proposed transport request and click Finish.
-
Replace the default code with the code snippet provided below and replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopyextend view entity ZITRAVEL_### with { _Travel.zztraveltype_zac as ZZTravelTypeZAC } -
Save
and activate
the changes.
-
Click anywhere in the editor and choose Open With > Data Preview from the context menu. The newly added field comes up in the data preview with the data filled.
-
- Step 9
While in the previous step, we added the logic in the ABAP class which updates the database table, in this step we will check how to dynamically calculate the values and add in a new field in the extended CDS view.
-
Add a new field
ZZDiscPriceZACwith the condition: Iftotal_price> 1000, apply a 10% discount. -
Copy the below code snippet and add below the exposed field
ZZTravelTypeZACABAPCopy@Semantics.amount.currencyCode: 'CurrencyCode' case when _Travel.total_price > 1000 then cast( _Travel.total_price as abap.dec(15,1) ) * cast( '0.9' as abap.dec(2,1) ) else cast( _Travel.total_price as abap.dec(15,1) ) end as ZZDiscPriceZAC -
Save
and activate
the changes.
-
Click anywhere in the editor and choose Open With > Data Preview from the context menu. A new field
ZZDiscPriceZACcomes up with the dynamic calculation.
-
- Step 10
You will expose
FlightDatefield from/DMO/I_BOOKING_Uand calculate the number of days left from today until the flight date.-
Below is the complete code of the extended view entity . Replace all occurrences of the placeholder
###with your group ID using the Replace All function (CTRL+F).ABAPCopyextend view entity ZITRAVEL_### with association [1..1] to /DMO/I_Booking_U as _ZZBooking on $projection.TravelId = _ZZBooking.TravelID { _Travel.zztraveltype_zac as ZZTravelTypeZAC, @Semantics.amount.currencyCode: 'CurrencyCode' case when _Travel.total_price > 1000 then cast( _Travel.total_price as abap.dec(15,1) ) * cast( '0.9' as abap.dec(2,1) ) else cast( _Travel.total_price as abap.dec(15,1) ) end as ZZDiscPriceZAC, _ZZBooking.FlightDate as ZZFlightDateZAC, case when _ZZBooking.FlightDate > $session.system_date then dats_days_between( _ZZBooking.FlightDate, $session.system_date ) else 0 end as ZZDaysRemainingZAC } -
Save
and activate
the changes.
-
Click anywhere in the editor and choose Open With > Data Preview from the context menu. There will be 4 custom fields added through Developer Extensibility.
-
- Step 11
What is the recommended way to extend a database table?
- Create your own ABAP package
- Create a Structure
- Create a database table
- Create data generator class
- Create a CDS view
- Extend the database table
- Create a new ABAP class to generate the values for the newly added field
- Extend the CDS View to consume the newly created custom field
- Add a new calculated field to the extended CDS view
- Extend the view with an association to CDS view entity
- Test yourself
- More Information