Virtual Elements in ABAP CDS View


Dear SAPLearners, in this blog post we will learn about Virtual Elements in ABAP CDS View and ABAP Code Exists.

Virtual Elements

Virtual elements are defined at CDS consumption view level by using specific @DataModel annotations. The calculation (or) processing logic of the field value is written in ABAP code.

Implementation of Virtual elements is done using an ABAP class is required and called as ABAP Code Exists API. You can use virtual element for read-only, as well as for transactional scenarios.

Virtual Elements, when to use in ABAP CDS view?

For instance, a business requirement need us to add a field to the CDS view which is calculated using ABAP code. To achieve such kind of requirements we can use virtual elements.

Use Cases

You can use virtual elements to implement following use cases:

  • To define calculated field values that are not part of database tables
  • Filtering of calculated field values
  • Sorting of calculated field values.

Virtual elements in ABAP CDS view, how to implement?

There are two steps in implementing a virtual element in CDS view. They are

  1. Adding Annotations
  2. Implementing ABAP Code Exists

1. Add Annotation to CDS view

Following are the annotations that needs to be add to the element of CDS consumption view

  • @ObjectModel.readOnly: true
  • @ObjectModel.virtualElement: true
  • @ObjectModel.virtualElementCalculatedBy: ‘ABAP:<code_exit_class>’

2. Implementing ABAP Code Exists

Create an ABAP code exit class and add the interface IF_SADL_EXIT_CALC_ELEMENT_READ.

Implement interface methods GET_CALCULATION_INFO and CALCULATE in custom code exit class to write the ABAP source code logic.

Demo Time

It’s time to demonstrate virtual elements in the CDS view with sample code.

In this example i am going to use the field “Discount” as a virtual element so that I calculate a discount based on ABAP logic written in code exit class.

Let’s dive into code.

1. Firstly add the virtual elements relevant annotations to CDS view. Here is the sample CDS view code.

@AbapCatalog.sqlViewName: 'ZCDSVIRELE'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Demo Virtual Elements'
@OData.publish: true
  as select from snwd_pd
  key node_key   as ProductGuid,
      product_id as ProductId,
      type_code  as CategoryCode,
      supplier_guid as SupplierGuid,
      tax_tarif_code as TaxTarifCode,
      price as Price,
      @ObjectModel.readOnly: true
      @ObjectModel.virtualElement: true
      @ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_DEMO_CODE_EXIT'
      cast( '' as abap.char(4)) as Discount 

Now moving on to the next step to create ABAP code exit class.

Also Read: Reduce ABAP CDS view development time using Templates.

2. Secondly, create a new ABAP class in SE24 and add the interface IF_SADL_EXIT_CALC_ELEMENT_READ.

3. Navigate to the methods section and implement below methods.

GET_CALCULATION_INFOProvides a list of all elements that are required for calculating the values of the virtual elements
CALCULATEExecutes the field calculation

4. Below is the code for method GET_CALCULATION_INFO. Add all fields that are need for calculation.

5. As a result fields require for calculation are available for calculation logic. Add the ABAP calculation logic in CALCULATE method.

Finally, it’s time to test the CDS view.

Virtual Elements is triggered only via SADL framework so if you execute the CDS view in Eclipse ADT using Data Preview (or) by calling the CDS view in an ABAP program you will NOT see any output.

The easiest way to test is to expose the CDS view as OData service and execute the service in Gateway Client.

Also Read: How to create OData service for CDS View

Hurray! ABAP code got executed and you can see the values in calculated field.

You can also consume this CDS view based OData service in a custom UI5 application or in a Fiori application based on Fiori Elements and the same result will be seen.


Congrats!! You have successfully learned about virtual elements in ABAP CDS view. A CDS with virtual elements is useful where the data model doesn’t have the required field and needs to be calculated based on the ABAP custom logic.

mail is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.

Like us on Facebook and follow us on Twitter.