Production Architectural Blueprint

SAP CDS Virtual Elements & SADL Calculation Architecture Guide

Domain: RAP Query Layer & Procedural Calculation Exits Standard: IF_SADL_EXIT_CALC_ELEMENT_READ Clean Core Tier: Tier-1 On-Stack

1. The Limits of SQL in Core Data Services

Core Data Services (CDS) push business logic down to the SAP HANA database engine with extraordinary efficiency. However, certain enterprise business calculations cannot be expressed purely in declarative SQL:

CDS Virtual Elements solve this architectural challenge by allowing developers to define non-persisted, transient fields in a CDS Projection View that are calculated on-the-fly by an ABAP class via the SADL (Service Adaptation and Description Layer) framework.

2. Architectural Decision Matrix: Virtual Elements vs Unmanaged Query vs Pure SQL

Evaluation Vector Pure SQL Expression (CDS) CDS Virtual Element (SADL Exit) RAP Custom Query Provider
Execution Location SAP HANA Database Engine ABAP Application Server (Post-SQL) ABAP Application Server (Full Query)
Procedural ABAP Logic No (Declarative SQL only) Yes (Full ABAP Cloud language) Yes (Custom paging/sorting/filtering)
Pagination ($top / $skip) Native HANA database pushdown Native HANA database pushdown Manually handled in ABAP code
Sorting & Filtering ($filter) Directly filterable in SQL Requires custom SADL filter transform Parsed manually via EML request
Standard Recommendation Default whenever possible Recommended for calculated UI fields Reserved for remote APIs / non-HANA data

3. The SADL Calculation Lifecycle

When an OData client or Fiori Elements app requests a page of 50 records from a projection view with a virtual element:

  1. Discovery: SADL inspects the CDS metadata and identifies @ObjectModel.virtualElement: true.
  2. Pre-Fetch Info: SADL invokes if_sadl_exit_calc_element_read~get_calculation_info on the calculation class. The method informs SADL which underlying database fields are needed for calculation.
  3. SQL Execution: SADL generates the optimized SQL SELECT ... FROM view LIMIT 50 OFFSET 0 including the requested dependency fields.
  4. Vectorized Calculation: SADL passes the returned 50-row internal table into if_sadl_exit_calc_element_read~calculate. The ABAP method loops across the table in-memory and sets the virtual element values.
  5. Response Serialization: SADL returns the enriched JSON payload to the client.

4. Implementing the Clean Core SADL Calculation Class

CLASS zcl_calc_sales_criticality DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_sadl_exit_calc_element_read.
ENDCLASS.

CLASS zcl_calc_sales_criticality IMPLEMENTATION.

  METHOD if_sadl_exit_calc_element_read~get_calculation_info.
    " Crucial: Prevent N+1 select queries by requesting prerequisite fields
    INSERT 'DELIVERYSTATUS' INTO TABLE et_requested_orig_elements.
  ENDMETHOD.

  METHOD if_sadl_exit_calc_element_read~calculate.
    DATA(lt_original_data) = it_original_data.

    LOOP AT lt_original_data ASSIGNING FIELD-SYMBOL(<row>).
      ASSIGN COMPONENT 'DELIVERYSTATUS' OF STRUCTURE <row> TO FIELD-SYMBOL(<status>).
      ASSIGN COMPONENT 'DELIVERYCRITICALITY' OF STRUCTURE <row> TO FIELD-SYMBOL(<crit>).
      
      IF <status> IS ASSIGNED AND <crit> IS ASSIGNED.
        CASE <status>.
          WHEN 'C'. <crit> = 3. " Green (Delivered)
          WHEN 'B'. <crit> = 2. " Yellow (Partially Delivered)
          WHEN 'A'. <crit> = 1. " Red (Blocked / Delinquent)
          WHEN OTHERS. <crit> = 0. " Grey
        ENDCASE.
      ENDIF.
    ENDLOOP.

    ct_calculated_data = CORRESPONDING #( lt_original_data ).
  ENDMETHOD.

ENDCLASS.

5. Critical Anti-Patterns to Avoid

⚠️ The Deadly N+1 Database Select Anti-Pattern

Never execute SELECT SINGLE or external HTTP requests inside the calculate loop! If a client pages through 100 records, executing a SELECT inside the loop results in 100 sequential roundtrips. Always register prerequisite fields in get_calculation_info or perform a single bulk FOR ALL ENTRIES lookup outside the row iteration.

Generate Your Virtual Elements In Seconds

Generate CDS annotations, SADL calculation exits, and unit test doubles with our interactive tool.

Launch Virtual Elements Architect ⚡ Get Architecture Suite (80% Off)