Skip to Content
Technical Articles
Author's profile photo Jan Braendgaard Petersen

ABAP Code Exits in CDS views

Introduction

In a CDS view it is possible to implement a Code Exit for an ABAP method call by defining a Virtual Element.

Please note that the call to the ABAP method will only be executed when the CDS view is consumed by something that handles annotations i.e. exposed as an Odata service, but not in transaction SE16N or the preview in Eclipse.

CDS view

First we create a standard CDS view, and add the virtual element we wish to connect to the ABAP method:

cast( '' as abap.char(255)) as text

ABAP class

The ABAP class must use the interface “if_sadl_exit_calc_element_read”, and implement the two methods “calculate” and “get_calculation_info”.

PUBLIC SECTION.
 INTERFACES if_sadl_exit_calc_element_read.

In the method “calculate” we can implement code to enrich the data from the CDS view. In the method “get_calculation_info” we can implement code to ensure that the data necessary for the use in method “calculate” are provided. If nothing is implemented in “get_calculation_info” all fields will be loaded with data prior to the call to the method “calculate”.

Note that it is not possible to add or delete rows in the dataset. SAP will do a check after the code exit to compare the number of rows, and any difference will result in an exception being raised – and a dump.

LOOP AT lt_calculated_data ASSIGNING FIELD-SYMBOL(<fs_data>).
  <fs_data>-text = 'Awesomeness'.
ENDLOOP.

Summary/use cases

When the ABAP method is called the complete dataset from the CDS view are passed. So access to all rows are possible. This could be used for calculations, or for more complex calls to methods or function modules like retrieving long texts from SO10.

References

SAP help: https://help.sap.com/viewer/cc0c305d2fab47bd808adcad3ca7ee9d/7.51.6/en-US/04e64a8ffffe49b8a8a5469796f397c0.html

Code Examples

CDS view ZV_SFLIGHT_CDS:

@AbapCatalog.sqlViewName: 'ZV_SFLIGHT_SQL'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS view on SFLIGHT'
define view zv_sflight_cds
  as select from sflight
{

  //sflight
  key carrid,
  key connid,
  key fldate,
      price,
      currency,
      planetype,
      seatsmax,
      seatsocc,
      paymentsum,
      seatsmax_b,
      seatsocc_b,
      seatsmax_f,
      seatsocc_f,
      @ObjectModel.readOnly: true
      @ObjectModel.virtualElement: true 
      @ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_CDS_FUNCTION'
      cast( '' as abap.char(255)) as text
}

ABAP class ZCL_CDS_FUNCTION:

CLASS zcl_cds_function DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

PUBLIC SECTION.
 INTERFACES if_sadl_exit_calc_element_read.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.



CLASS zcl_cds_function IMPLEMENTATION.
  METHOD if_sadl_exit_calc_element_read~calculate.

    DATA:
      lt_calculated_data TYPE STANDARD TABLE OF zv_sflight_cds WITH DEFAULT KEY.

    MOVE-CORRESPONDING it_original_data TO lt_calculated_data.

    LOOP AT lt_calculated_data ASSIGNING FIELD-SYMBOL(<fs_data>).
        <fs_data>-text = 'Awesomeness'.
    ENDLOOP.

    MOVE-CORRESPONDING lt_calculated_data TO ct_calculated_data.

  ENDMETHOD.

  METHOD if_sadl_exit_calc_element_read~get_calculation_info.

  ENDMETHOD.

ENDCLASS.

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Nivardo Edwin Tahua Quijano
      Nivardo Edwin Tahua Quijano

      Hi Jan, with virtual element annotations for calculate columns the results is only show when the CDS is called from a service or exits other shape? Because I’m create a cDs with column calculate and the value is show in Blank when execute the cds by Se16n o preview data in eclipse.

       

      thank you!!

      Author's profile photo Jan Braendgaard Petersen
      Jan Braendgaard Petersen
      Blog Post Author

      Hello Nivardo, yes you have to consume the CDS through a service or similar. It will not work through SE16 or the preview in Eclipse.

       

      Author's profile photo Sheilesh Ingale
      Sheilesh Ingale

      Hi Jan,

      Interesting info. Will it work via "select" on CDS view in ABAP? Or is this the same case as SE16?

      Author's profile photo Jan Braendgaard Petersen
      Jan Braendgaard Petersen
      Blog Post Author

      Hello Sheilesh Ingale , I haven't tried that yet, but if you do please let me know.

       

      Regards,

      Jan

      Author's profile photo Alexandra Marinescu
      Alexandra Marinescu

      Hi Jan,

      Can we add virtual elements via extension to a standard CDS view used in a predefined VDM? If so, could the OData service be regenerated so it includes the new fields?

      Author's profile photo Sijin Chandran
      Sijin Chandran

      Below blog covers this topic in much detail.

      https://blogs.sap.com/2019/04/26/fiori-elements-utilizing-cds-with-virtual-elements/

      Author's profile photo Emmanouil Kouvaritakis
      Emmanouil Kouvaritakis

      hi Jan,

      do you know if this functionality can be used by Analytical Queries (RSRT or AFO)?

      Author's profile photo Klaus Reiner
      Klaus Reiner

      I am just creating an extractor based on ABAP-CDS. There I get the warning messages,

      that these annotations are not allowed in there. Do you have an idea what the prerequisited

      are to get them run there as well? BR, Klaus

      Author's profile photo Orhan Er
      Orhan Er

      Hello Jan,

       

      First of all, thank you very much for the article. I have a question.
      How can we assign a parameter (in Javascript) in UI5 (not with Fiori) to this Virtual element. Because then I want to access the data assigned to this Virtual element from inside the Class.
      Does it have such a feature?

      I'm asking this question because I couldn't find an answer to the question I opened here.

      Access RAP CDS behavior implementations (backend), via the Business App.Studio-UI5 (Frontend) button

       

      Thank you.

      Orhan

      Author's profile photo Abel Dicka Ngoh
      Abel Dicka Ngoh

      Many thanks for your post , I didn't implemented it yet ... but I'm very happy finding this trick