CDS Annotations in SAP – 3

CDS annotations are metadata tags that can be applied to SAP’s ABAP CDS views. These annotations assist specify the view’s behavior on multiple levels, such as how the data is shown in applications, how it interacts with other systems, and how it is processed within SAP HANA. Annotations are typically defined with the @ symbol followed by the annotation name and value.

Consumption-Annotations in SAP ABAP CDS Views

Consumption annotations aim to facilitate the construction of user interfaces and analytical solutions by offering a declarative method for specifying data consumption needs. Instead of writing sophisticated UI or business logic code, consumption annotations allow developers to directly manage important behaviors within the CDS view, such as how data is displayed, filtered, or aggregated.

@Consumption.defaultValue CDS Annotations

The main purposes of using @Consumption.defaultValue are:

  • UI Behavior: To pre-set values for UI elements like search filters or input fields, improving user experience.
  • Data Initialization: To provide default values for fields that may not be explicitly set when creating or editing data (e.g., in forms or reports).
  • Business Logic Enrichment: To ensure consistent and predefined behavior when users or external systems interact with the data model.
@AbapCatalog.sqlViewName: 'ZPRODUCT_CDS'
@OData.publish: true
@UI.lineItem: [
  { position: 10, label: 'Product Name' },
  { position: 20, label: 'Product Category' },
  { position: 30, label: 'Quantity' }
]
define view ZProduct_CDS_View as select from mara
  association [0..1] to zproduct_category as _category on _category.id = mara.category
{
  key mara.matnr as ProductID,
  mara.maktx as ProductName,
  
  @Consumption.defaultValue: 'General'
  mara.mtart as ProductCategory,      // Default value 'General' for Product Category
  
  @Consumption.defaultValue: '10'
  mara.menge as Quantity             // Default value '10' for Quantity
}

@Consumption.derivation.binding Annotations in CDS

The @Consumption.derivation.binding annotation in SAP Core Data Services (CDS) is used to bind a derived field (calculated or dynamic) to another field or UI element. This enables the dynamic generation of values in user interfaces or external systems based on other fields or parameters. The derivation process allows the value of one field to be calculated from others, providing more flexibility in how data is presented and interacted with.

The @Consumption.derivation.binding annotation is most commonly used in scenarios where you want a field to be automatically populated based on other data, such as when the value of a field needs to be calculated or set dynamically in an SAP Fiori application or other external systems. It allows you to bind derived or calculated fields to UI elements or other fields based on a condition or rule.

@AbapCatalog.sqlViewName: 'ZPRODUCT_CDS'
@OData.publish: true
define view ZProduct_CDS_View as select from mara
  association [0..1] to zproduct_category as _category on _category.id = mara.category
{
  key mara.matnr as ProductID,
  mara.maktx as ProductName,
  mara.price as Price,
  mara.discount as Discount,
  
  @Consumption.derivation.binding: 'Price - Discount'
  (Price - Discount) as PriceAfterDiscount  // Derived value: Price - Discount
}

@Consumption.derivation.derivationFilter CDS Annotations

The @Consumption.derivation.derivationFilter annotation in SAP Core Data Services (CDS) is used to control the filtering behavior of derived fields in the context of derivations. This annotation helps define specific filtering criteria for the derived or calculated values, which can be critical in ensuring that these derived fields are computed correctly and meet certain business logic or filtering requirements.

When you have a field that is derived based on other fields (for example, a calculated total or a value that depends on certain conditions), the @Consumption.derivation.derivationFilter annotation allows you to apply filters to control when or how the derivation of the value takes place. This annotation can be particularly useful in scenarios where derived fields need to be computed differently depending on certain conditions or when the derived field’s value should be excluded from calculations in some cases.

@AbapCatalog.sqlViewName: 'ZSALESORDER_CDS'
@OData.publish: true
define view ZSalesOrder_CDS_View as select from zsales_order
{
  key zsales_order.order_id as OrderID,
  zsales_order.product_name as ProductName,
  zsales_order.quantity as Quantity,
  zsales_order.unit_price as UnitPrice,
  zsales_order.discount as Discount,

  @Consumption.derivation.binding: 'Quantity * UnitPrice - (Quantity * UnitPrice * Discount)'
  @Consumption.derivation.derivationFilter: 'Discount > 0'
  (Quantity * UnitPrice - (Quantity * UnitPrice * Discount)) as TotalAmount  // Derived TotalAmount with Discount filter
}

TotalAmount: This field is derived by multiplying Quantity and UnitPrice and then subtracting the discount. The @Consumption.derivation.binding annotation defines this calculation.@Consumption.derivation.derivationFilter: This annotation specifies that the calculation of TotalAmount should only occur if the Discount is greater than 0. If the Discount is 0 or NULL, the derivation will not be applied, and TotalAmount will not be calculated.Usage: This ensures that a total amount is only calculated for orders that have a valid discount, effectively ignoring records where the discount is 0 or missing.

@Consumption.derivation.lookupEntity Annotations in CDS

The @Consumption.derivation.lookupEntity annotation in SAP Core Data Services (CDS) is a powerful feature that enables you to derive a field based on a lookup operation from another entity. Essentially, it allows you to perform a lookup to another CDS view (or entity) and derive a value by referencing that entity. This is particularly useful when you need to enrich the data from the primary CDS Annotations view with additional information from related or external datasets, like referencing customer details, product descriptions, or other business-related attributes.

In simpler terms, @Consumption.derivation.lookupEntity enables you to fetch related data from a different CDS view or table to populate a derived field in your main CDS view.

Purpose of @Consumption.derivation.lookupEntity

The main purposes of using the @Consumption.derivation.lookupEntity annotation are:

  • Enriching Data: Fetch additional information from another CDS view, table, or external entity and use that data for derived fields.
  • Cross-Entity Lookups: Perform lookups to link data from different entities, like fetching a product name from a Product table based on a product ID in the main Order CDS view.
  • Data Augmentation: Dynamically augment or extend the dataset with additional information, making it more valuable for analytics, reporting, and user interfaces.
@AbapCatalog.sqlViewName: 'ZORDER_CDS'
@OData.publish: true
define view ZOrder_CDS_View as select from zsales_order
  association [0..1] to ZProduct_CDS as _product on _product.ProductID = zsales_order.ProductID
{
  key zsales_order.OrderID as OrderID,
  zsales_order.ProductID as ProductID,
  zsales_order.Quantity as Quantity,
  zsales_order.UnitPrice as UnitPrice,
  
  @Consumption.derivation.lookupEntity: 'ZProduct_CDS'
  _product.ProductName as ProductName  // Lookup to the Product CDS View to get Product Name
}

Explanation:

  • ProductName: The ProductName field is derived by looking up the related ProductName from the ZProduct_CDS CDS view. The lookup is performed using the ProductID field in the current ZSalesOrder view and the associated ProductID in the ZProduct_CDS view.
  • @Consumption.derivation.lookupEntity: The annotation specifies that the ProductName should be derived by looking up the ZProduct_CDS view, enriching the SalesOrder data with the related ProductName.
  • Association: The association (_product) is used to link the ZSalesOrder and ZProduct_CDS views based on ProductID. The derived ProductName is fetched from this associated view.

Consumption.derivation.procedure CDS Annotations

The @Consumption.derivation.procedure annotation in SAP Core Data Services (CDS) is used to define a derived field whose value is calculated by invoking a stored procedure. This annotation is useful when the business logic for deriving a value is complex and cannot be easily expressed with a simple calculation or association. Instead of calculating the value within the CDS view itself, the value is derived by calling a stored procedure, which encapsulates the complex logic, potentially involving multiple tables, calculations, or other external systems.

This allows for the separation of business logic (stored in the procedure) from the data layer (in the CDS view), making the system more modular and easier to maintain.

Purpose of @Consumption.derivation.procedure

The primary purposes of using the @Consumption.derivation.procedure annotation are:

  • Encapsulate Complex Logic: If the derivation of a field involves complex logic, such as multi-step calculations or database calls, you can offload this logic to a stored procedure.
  • Reuse Business Logic: If the same calculation needs to be used across multiple CDS views or different applications, encapsulating it in a stored procedure allows for reusability.
  • Maintainability: Stored procedures can be more maintainable and easier to debug, especially for complex business rules that cannot be directly expressed in CDS syntax.
@AbapCatalog.sqlViewName: 'ZSALESORDER_CDS'
@OData.publish: true
define view ZSalesOrder_CDS as select from zsales_order
{
  key zsales_order.OrderID as OrderID,
  zsales_order.Quantity as Quantity,
  zsales_order.UnitPrice as UnitPrice,
  zsales_order.CustomerID as CustomerID,

  @Consumption.derivation.procedure: 'ZCALC_TOTAL_AMOUNT'
  TotalAmount as TotalAmount  // Derived field using a procedure
}

Explanation:

  • TotalAmount: The TotalAmount field is derived by calling an ABAP procedure named ZCALC_TOTAL_AMOUNT.
  • @Consumption.derivation.procedure: The annotation specifies that the TotalAmount will be derived using the ZCALC_TOTAL_AMOUNT procedure, which may contain logic such as applying a discount based on the CustomerID or some other complex business rules.

Consumption.derivation.resultElement CDS Annotations

The @Consumption.derivation.resultElement annotation in SAP Core Data Services (CDS) is used to define derived fields in a CDS view where the result of a derivation (e.g., calculation, transformation, lookup, or procedure) is mapped to a specific element or target field. It allows the developer to specify how and where the derived values should be stored or returned, making it a valuable tool for customizing the consumption layer of CDS views, particularly for analytical applications like SAP Fiori or reporting.

This annotation is part of the derivation concept, where the result of a complex calculation, procedure, or lookup is stored in a specific result element. You can use it to manage how complex or calculated values should be handled when consumed by the client application or other downstream systems.

Purpose of @Consumption.derivation.resultElement

The main purposes of using the @Consumption.derivation.resultElement annotation are:

  1. Map Derived Values: This annotation allows the developer to map the results of derivation expressions (like calculations or logic) to specific fields or result elements.
  2. Handle Complex Derivations: You can derive values based on complex business logic or rules and then assign the result to a specific result element in the CDS view.
  3. Provide Custom Fields for Consumption: For applications consuming the CDS view (like SAP Fiori), you can specify how derived fields should be exposed or structured for better usability in analytical scenarios.
  4. Enhance Reusability: By using the resultElement, you enable reusable and modular components for derived fields that can be referenced by different applications or reports.
@AbapCatalog.sqlViewName: 'ZSALESORDER_CDS'
@OData.publish: true
define view ZSalesOrder_CDS as select from zsales_order
{
  key zsales_order.OrderID as OrderID,
  zsales_order.Quantity as Quantity,
  zsales_order.UnitPrice as UnitPrice,

  @Consumption.derivation.resultElement: 'TotalAmount'
  (zsales_order.Quantity * zsales_order.UnitPrice) as TotalAmount  // Derived field
}

Explanation:

  • TotalAmount: The TotalAmount field is calculated by multiplying Quantity and UnitPrice.
  • @Consumption.derivation.resultElement: This annotation specifies that the result of the derivation (multiplication of Quantity and UnitPrice) should be mapped to the TotalAmount field.

Consumption.filter.defaultValue CDS Annotations

The @Consumption.filter.defaultValue annotation in SAP Core Data Services (CDS) is used to specify a default value for a filter field in a CDS view when the view is consumed by external applications such as SAP Fiori, SAP BW, or SAP Analytics Cloud (SAC). This annotation is especially helpful for pre-setting filter values, which reduces the need for users to manually set filters every time they access the data.

By applying this annotation, developers can ensure that certain filter criteria are automatically applied when the view is queried, improving the user experience and ensuring data consistency.

Purpose of @Consumption.filter.defaultValue

The primary purposes of using @Consumption.filter.defaultValue are:

  1. Pre-set Filter Criteria: It defines default values for filters that are automatically applied when the CDS view is consumed. This eliminates the need for users to manually apply filters.
  2. Simplify User Interactions: It enhances the user experience by pre-populating filter fields with values that are commonly used or relevant.
  3. Enforce Business Logic: Default values can enforce certain business logic, ensuring that only relevant records are shown by default.
  4. Improve Reporting: In reporting scenarios, such as with SAP Fiori apps or SAC, setting default values for filters allows for more focused and relevant reports.
@AbapCatalog.sqlViewName: 'ZSALESORDER_CDS'
@OData.publish: true
define view ZSalesOrder_CDS as select from zsales_order
{
  key zsales_order.OrderID as OrderID,
  zsales_order.OrderDate as OrderDate,
  zsales_order.OrderStatus as OrderStatus,
  
  @Consumption.filter.defaultValue: 'Open'
  @Consumption.filter: {OrderStatus}
}

Explanation:

  • @Consumption.filter.defaultValue: 'Open': This annotation sets the default value of the OrderStatus filter to 'Open'. This means that when the CDS view is consumed, it will automatically show only orders that have the status 'Open' by default.
  • @Consumption.filter: This annotation marks the OrderStatus field as a filter that can be used by the consuming application (like SAP Fiori, SAP Analytics Cloud, or SAP BW).

Consumption.filter.hidden CDS Annotations

The @Consumption.filter.hidden annotation in SAP Core Data Services (CDS) is used to control the visibility of filter fields in the user interface when consuming the CDS view in applications such as SAP Fiori or SAP Analytics Cloud (SAC). By setting the @Consumption.filter.hidden annotation, developers can ensure that certain filter fields are not visible or accessible to end users in the UI, even though they are still available for use within the CDS view or underlying application.

Purpose of @Consumption.filter.hidden

The @Consumption.filter.hidden annotation is used for the following purposes:

  1. Hide Filters from User Interface: Some filters, while necessary for data retrieval or application logic, should not be exposed to the user.
  2. Enforce Data Consistency: Developers can apply hidden filters to enforce consistent filtering logic (such as showing only active records or filtering by user-specific data) while keeping the filter hidden from users.
  3. Improve User Experience: By hiding irrelevant or unnecessary filters from the user interface, the user experience is streamlined, making the application more intuitive and less cluttered.
@AbapCatalog.sqlViewName: 'ZSALESORDER_CDS'
@OData.publish: true
define view ZSalesOrder_CDS as select from zsales_order
{
  key zsales_order.OrderID as OrderID,
  zsales_order.OrderDate as OrderDate,
  zsales_order.OrderStatus as OrderStatus,

  @Consumption.filter.hidden: true
  @Consumption.filter: {OrderDate}
}

Explanation:

  • @Consumption.filter.hidden: true: This annotation hides the OrderDate field from the filter panel in the consuming application (e.g., SAP Fiori or SAP Analytics Cloud). Users will not be able to filter the data by OrderDate, but the field will still be part of the view and can be used in queries.
  • @Consumption.filter: {OrderDate}: This annotation exposes the OrderDate field as a filter, but since it’s hidden with @Consumption.filter.hidden: true, users will not see or interact with it in the UI.

Consumption.filter.hierarchyBinding CDS Annotations

The @Consumption.filter.hierarchyBinding annotation is a part of SAP Core Data Services (CDS) that enables you to bind hierarchical fields in a CDS view to a filter, allowing users to filter data based on hierarchical relationships. This is particularly useful when working with data that has parent-child relationships, such as organizational structures, categories, regions, or product hierarchies.

This annotation is commonly used in consumer applications like SAP Fiori, SAP Analytics Cloud (SAC), and other reporting tools to provide dynamic, hierarchical filtering options to the users.

Purpose of @Consumption.filter.hierarchyBinding

The @Consumption.filter.hierarchyBinding annotation serves the following purposes:

  1. Hierarchical Filtering: It allows you to bind a field representing a hierarchy (such as a product category, organization unit, or geographical region) to a filter, enabling users to filter data based on hierarchical levels (parent-child relationships).
  2. Dynamic Drill-Down: It provides a way for users to drill down into data by selecting higher-level categories (e.g., a region or department) and then filtering data based on subcategories or child elements.
  3. Intuitive User Experience: It simplifies the user experience by automatically applying hierarchical relationships to filters, helping users to navigate and filter data in a more intuitive and structured manner.

Syntax of @Consumption.filter.hierarchyBinding

The basic syntax for using the @Consumption.filter.hierarchyBinding annotation is as follows:

@Consumption.filter.hierarchyBinding: [
  {
    field: <field_name>, 
    type: <hierarchy_type>
  }
]

Where:

  • <field_name>: This refers to the CDS field (or column) that represents the hierarchical data, such as a category, region, or organizational unit.
  • <hierarchy_type>: Specifies the type of the hierarchy filter, which can be:
    • Hierarchy: This type represents a hierarchical filter, allowing users to filter by both parent and child nodes.
    • Flat: This type treats the hierarchy as a flat list, where the relationship between parent and child nodes is not considered when filtering.
@AbapCatalog.sqlViewName: 'ZPRODHIERARCHY_CDS'
@OData.publish: true
define view ZProductHierarchy_CDS as select from zproduct_hierarchy
{
  key zproduct_hierarchy.ProductID as ProductID,
  zproduct_hierarchy.ProductName as ProductName,
  zproduct_hierarchy.Category as Category,
  zproduct_hierarchy.SubCategory as SubCategory,

  @Consumption.filter.hierarchyBinding: [
    { 
      field: Category,
      type: 'Hierarchy'
    },
    { 
      field: SubCategory,
      type: 'Hierarchy'
    }
  ]
}

Leave a Comment