Core Data Services (CDS) ABAP

Virtual Elements

Virtual Elements – Why?

Use Case

  • CDS can do a lot
  • … but not everything

Example

Some logic is so complex that implementing it in CDS would be very difficult,
while an existing ABAP building block can deliver the result with a simple call.

Consideration

  • Fall back to ABAP!

Extending CDS

Classic Syntax

    @ObjectModel.virtualElement: true
    @ObjectModel.virtualElementCalculatedBy: 'ZCL_DEMO_CDS_VIRT'
    cast( '' as abap.char(12))  as approver,

Syntax in Projection Views

    @ObjectModel.virtualElementCalculatedBy: 'ZCL_DEMO_CDS_VIRT'
    virtual Approver : abap.char( 12 ),

Logic in ABAP

Create a Class

  • Use the interface IF_SADL_EXIT_CALC_ELEMENT_READ
  • Additional optional interfaces:
    • IF_SADL_EXIT_FILTER_TRANSFORM
    • IF_SADL_EXIT_SORT_TRANSFORM

Implement the Class

  • Implement GET_CALCULATION_INFO
    → Defines which fields are required
  • Implement CALCULATE
    → This is where the magic happens!

Example Implementation

Determining Required Fields

  METHOD if_sadl_exit_calc_element_read~get_calculation_info.

    LOOP AT it_requested_calc_elements INTO DATA(lv_req_elem).

      CASE lv_req_elem.

        WHEN 'GROSS_PRICE'.

          INSERT CONV #( 'NET_PRICE' ) INTO TABLE et_requested_orig_elements.
          INSERT CONV #( 'TAXES' ) INTO TABLE et_requested_orig_elements.

      ENDCASE.

    ENDLOOP.

  ENDMETHOD.

Calculating the Fields

METHOD if_sadl_exit_calc_element_read~calculate.

  DATA: lt_data TYPE TABLE OF ZC_Output.

  lt_data = CORRESPONDING #( it_original_data ).

  LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<line>).

    <line>-gross_price = <line>-net_price * <line>-taxes.

  ENDLOOP.

  ct_calculated_data = CORRESPONDING #( lt_data ).

ENDMETHOD.

Background and Limitations

How does it work?

  • CDS is passed from ABAP to the database
  • The database cannot execute ABAP
  • So where is the ABAP code executed?

Limitations

  • Virtual elements are provided via the SADL layer
  • SADL (Service Adaptation Definition Language) translates OData → SQL
  • Therefore: Virtual elements are not available via direct ABAP SQL!

Additional Options When CDS Alone Is Not Enough

  • Redefine RDS in SEGW + custom post-processing implementation
  • Use CDS Table Functions