Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
JanBraendgaard
Active Participant


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/04e64a8ffffe49b8a8a5469796...


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.
10 Comments