ABAP RAP Advanced

Virtual Elements

Implementing Dynamic Fields in ABAP

What Are Virtual Elements?

  • Fields that are not persistently stored in the database
  • Values are calculated at runtime by an ABAP class
  • Used for computed values, technical information, or external data

virtual element logic flow

Old Syntax: Virtual Element in CDS View

@ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_MY_VIRTUAL_ELEMENT_EXIT'
virtual VirtualAmount : abap.curr(15,3);
  • Interface: IF_SADL_EXIT_CALC_ELEMENT_READ implemented by the class
  • Methods get_calculation_info and calculate control the calculation
  • Mainly used in classic CDS views without define root view entity

New Syntax: Virtual Element in RAP Projections

define root view entity ZC_MY_ENTITY
provider contract my_provider_contract
as projection on ZI_MY_ENTITY
{
  key ID,
  Name,
  @ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_MY_VIRTUAL_ELEMENT_EXIT'
  virtual VirtualAmount : abap.curr(15,3)
}
  • Root view entities with provider contracts allow virtual elements
  • New projects should prefer this approach for better integration
  • Supported in RAP and Fiori Elements for display and filtering

Implementing the ABAP Class

  • Class implements the IF_SADL_EXIT_CALC_ELEMENT_READ interface
  • GET_CALCULATION_INFO defines required fields for calculation
  • CALCULATE fills values into the provided data structure

Tips and Notes

  • Virtual elements are read-only; no persistence possible
  • Filtering on virtual elements is only partially performant
  • Integration with Fiori Elements works out-of-the-box
  • For simple calculations or aggregations, consider solving in CDS

Summary

  • Virtual elements extend the RAP data model without DB overhead
  • Old and new syntax coexist; new syntax is recommended
  • ABAP class acts as the core for calculation logic
  • Important for dynamic, computed fields in UI and services