This is a test message to test the length of the message box.
Login
|
ABAP RAP Virtual Elements
Created by Software-Heroes

RAP - Virtual Elements

4595

There are virtual elements around the projections, we show you how to use and implement them to extend your RAP object.

Advertising


We already discussed virtual elements in CDS views in an older article. Today we present another variant, which is mainly used in projections of RAP objects.

 

Definition

The elements receive their own keyword in the CDS view and again receive the annotation to indicate the implementing class. These virtual elements can only be used in the projection layer and will result in an error for a view or standard entity.

@ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_BS_DEMO_CRAP_VE_EXIT' 
virtual NumberOfPositions : abap.int4,

 

Class

The class is implemented again accordingly with the interface IF_SADL_EXIT_CALC_ELEMENT_READ. It is sufficient to fill the CALCULATE method with content. The example implementation of the method could look like this:

LOOP AT it_requested_calc_elements INTO DATA(ld_virtual_field).
  LOOP AT ct_calculated_data ASSIGNING FIELD-SYMBOL(<ls_calculated_data>).
    DATA(ld_tabix) = sy-tabix.
    ASSIGN COMPONENT ld_virtual_field OF STRUCTURE <ls_calculated_data> TO FIELD-SYMBOL(<ld_field>).

    DATA(ls_original) = CORRESPONDING ZBS_C_RAPCInvoice( it_original_data[ ld_tabix ] ).

    IF ls_original-Partner = '1000000002'.
      <ld_field> = 999.

    ELSE.
      SELECT FROM zbs_dmo_position
        FIELDS COUNT( * )
        WHERE document = @ls_original-Document
        INTO @<ld_field>.

    ENDIF.
  ENDLOOP.
ENDLOOP.

 

The method loops over the requested items and then adds the content that was requested. To derive our information from the data, we assign it to an internal variable. Since the data types were defined as ANY, we have to go this route to address the elements by their names. In the end, for a specific partner we set the number to 999 and for all other partners we determine the number of positions. The overall implementation would then look like this:

CLASS zcl_bs_demo_crap_ve_exit DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    INTERFACES if_sadl_exit_calc_element_read.
ENDCLASS.


CLASS zcl_bs_demo_crap_ve_exit IMPLEMENTATION.
  METHOD if_sadl_exit_calc_element_read~calculate.
    LOOP AT it_requested_calc_elements INTO DATA(ld_virtual_field).
      LOOP AT ct_calculated_data ASSIGNING FIELD-SYMBOL(<ls_calculated_data>).
        DATA(ld_tabix) = sy-tabix.
        ASSIGN COMPONENT ld_virtual_field OF STRUCTURE <ls_calculated_data> TO FIELD-SYMBOL(<ld_field>).

        DATA(ls_original) = CORRESPONDING ZBS_C_RAPCInvoice( it_original_data[ ld_tabix ] ).

        IF ls_original-Partner = '1000000002'.
          <ld_field> = 999.

        ELSE.
          SELECT FROM zbs_dmo_position
            FIELDS COUNT( * )
            WHERE document = @ls_original-Document
            INTO @<ld_field>.

        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDMETHOD.


  METHOD if_sadl_exit_calc_element_read~get_calculation_info.
  ENDMETHOD.
ENDCLASS.

 

Usage

Within RAP, additional information can be given to the frontend that is not available in the data model. For technical fixed values, readable content could be transferred in this way. The result of the example shown above looks like this:

 

Conclusion

As always, we have committed the current variant to GitHub so that you can understand the changes and examples. Virtual elements are implemented quite simply, but information from JOIN and association should be used preferably.


Included topics:
RAPBTPVirtual ElementsVIRTUALREX2
Comments (1)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


RAP - Navigation (App to App)

Category - ABAP

How can you navigate to another application within your Fiori application without the user having to manually switch apps? Let's take a look at navigation between Fiori apps.

09/01/2026

ABAP in Practice - Creating a Setup App

Category - ABAP

No more SAP GUI? How do we then create a static setup app while still remaining flexible on the ABAP side for further implementation? Here's a practical example.

08/28/2026

RAP - Singleton Pattern

Category - ABAP

The Singleton pattern in rap has been around for a while, but how exactly does it work and how can you use it for other purposes? Let's take a closer look at the details in this article.

08/25/2026

RAP - Native File Upload

Category - ABAP

Do you want to upload a file to your RAP application and then process it? The official solution for uploading and downloading is still pending, but it's already possible today with a small workaround.

08/18/2026

RAP - Feature Control (Part 2)

Category - ABAP

What about the feature controls in RAP, and what changes have been made since then? Here, we'll take another look at the features for our sales app and the updates that have been implemented.

08/14/2026