Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
AbhishekSharma
Active Contributor
16,139
Hello,

In this beginner blog post we are going to see how to work with Virtual Elements in ABAP Restful Application Programming Model.

Ref: https://help.sap.com/docs/BTP/923180ddb98240829d935862025004d6/c65942c284dd490a9c3791630d4d4e41.html

 



 

What is Virtual Element :


Virtual Elements are as name suggests "Virtual" which does not exists. Virtual Elements does not exists in actual Data Base table or Persistent Table used to fetch the records and show on Fiori Application.

Virtual Elements are used to map with values which are calculated at ABAP runtime and show them on Fiori Application.

  1. Virtual Elements are always defined in Projection View or Consumption View.

  2. Virtual Elements can not be Key field of CDS View

  3. Virtual Elements can not be used in Aggregations


 

How to Create Virtual Elements :


Virtual Elements are always created using keyword Virtual inside Projection or Consumption View.

Value calculation for Virtual Elements are done using ABAP Class which Implements special Interface IF_SADL_EXIT_CALC_ELEMENT_READ.

Interface IF_SADL_EXIT_CALC_ELEMENT_READ provides two different methods as below:

  1. GET_CALCULATION_INFO : Method is used to collect data before sending to CALCULATE method for actual business logic implementation. This GET_CALCULATE_INFO method always gets called before the CALCULATE method.

  2. CALCULATE :Method is used to Implement actual business logic using Original data and make Virtual Elements available with updated/new/calculated value to frontend. This method always gets trigger once GET_CALCULATED_INFO method ends processing.


Creating Virtual Element in CDS View Entity



To create Virtual Element we use @ObjectModel annotation to define a Class name which will be responsible to calculate Virtual Elements value.

In our Example we are creating Class with name ZCL_CALCULATE

With Virtual keyword we have defined Virtual Element with name BonusAmount of type abap.int4.

Create Class:



 

Implementing Interface


Implement Interface which provides methods to build Virtual Element data


 

The 2 different methods are created through Implementation of Interface. These 2 methods are Independent of each other and its not mandatory to have code in both the methods. It all depends on Business requirement.

 

Updating CALCULATE Method:


 


 

Important

Defining Internal table to collect data for Processing. Type of this Internal table would be name of Consumption or Projection View.

If you are using old DDIC views then type of this Internal table would be name of SQL View which gets created when we activate DDIC View.

All available data from persistent table or from CDS View will be available in Importing parameter IT_ORIGINAL_DATA

Once processing is complete and Virtual element is updated with calculated values, everything from updated Internal table needs to be transferred to Changing parameter table CT_CALCULATED_DATA

Activate Class

 

Update Metadata Extension File


Since we are using Metadata Extension file to display Columns on Fiori Application. We need to add new Virtual Element column BonusAmount.

 


 

Activate Metadata Extension File

Run Application


 


We see new column added (Total Pay) where we are displaying calculated Salary.

Professors :  Bonus Amount 500 - Total Pay = 15500

Teachers : Bonus Amount 1000 - Total Pay = 11000

 

These Virtual Element field swill also be available in oData service.

 

Thanks-

Abhishek

 

 
5 Comments
Labels in this area