SAP ABAP CDS View – Extend (Extend a CDS View)
In SAP ABAP CDS views, view extension allows you to enhance an existing CDS view by adding additional fields or modifying annotations, without changing the original view itself. This feature helps maintain the integrity of standard views while enabling customization for specific business needs.
🔹 Syntax for Extending a CDS View
To extend an existing CDS view, we use the EXTEND VIEW
keyword.
Basic Syntax of Extending a CDS View
@AbapCatalog.sqlViewName: 'ZFLIGHT_EXT'@AccessControl.authorizationCheck: #NOT_REQUIREDextend view ZFLIGHT_CDS with ZFLIGHT_EXT { additional_field1 as new_field1, additional_field2 as new_field2}
Explanation:
ZFLIGHT_CDS
is the base view you’re extending.ZFLIGHT_EXT
is the extended view, where you can add new fields or annotations.additional_field1
andadditional_field2
are new fields you’re adding to the view.
🔹 Example of Extending a CDS View with Additional Fields
@AbapCatalog.sqlViewName: 'ZFLIGHT_EXTENDED'@AccessControl.authorizationCheck: #CHECKextend view ZFLIGHT_CDS with ZFLIGHT_EXTENDED { price * 0.9 as discounted_price, -- Applying a 10% discount 'Y' as new_flag -- New field indicating active flights}
Explanation:
- Adds a calculated field (
discounted_price
) for flights with a 10% discount. - Adds a static field (
new_flag
) to mark active flights with'Y'
.
🔹 Extending a CDS View with Associations
extend view ZFLIGHT_CDS with ZFLIGHT_ASSOCIATED { association [0..1] to scarr as _Carrier on _Carrier.carrid = ZFLIGHT_CDS.carrid}
Explanation:
- Adds an association to the
scarr
table to retrieve the carrier name for each flight.
🔹 Extending CDS Views with Annotations
@AbapCatalog.sqlViewName: 'ZFLIGHT_ANNOTATED'@AccessControl.authorizationCheck: #CHECKextend view ZFLIGHT_CDS with ZFLIGHT_ANNOTATED { @UI.lineItem: [{ position: 10 }] price as flight_price}
Explanation:
- Adds an annotation for UI (Fiori) applications to display the
price
field in a specific position on the line item.
🔹 Key Benefits of Extending CDS Views
- Non-Invasive Customization: Enhances or customizes existing views without modifying the original object, making it upgrade-safe.
- Flexibility: Allows adding custom logic, annotations, or fields to standard views.
- Improved Maintainability: You can maintain and upgrade standard CDS views while adding enhancements tailored to your business needs.
🔹 When to Use CDS View Extension?
- When you need to add custom fields or logic to a standard CDS view (e.g., adding calculated fields, UI annotations, etc.).
- When you want to enhance the existing functionality without altering the core business logic defined in the original view.
- When building customer-specific reports or applications in Fiori or SAP S/4HANA environments.