How to use Virtual Elements in ABAP Restful Application Programming Model
In this beginner blog post we are going to see how to work with Virtual Elements in ABAP Restful Application Programming Model.
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.
- Virtual Elements are always defined in Projection View or Consumption View.
- Virtual Elements can not be Key field of CDS View
- 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:
- 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.
- 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.
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:
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
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
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.
not implementing the method get_calculation_info will lead to inconsistent behavior and as described in the documentation it is a necessary step: https://help.sap.com/docs/BTP/923180ddb98240829d935862025004d6/4430bddf68ee4258bf629759f0ff6ab5.html
In your example you rely on the information stored in the property "Role". If the application or OData consumer of your service does not request that property, the data will not be retrieved from the database, resulting in empty values for your calculation method. Your logic is relying on the fact that the consumer is always requesting the property "Role".
It is therefore necessary that you always have to return the required element "Role" in et_requested_orig_elements of method get_calculation_info.
The following statement is not correct:
You are not able or allowed to manipulate data. You can add additional fields to the requested element list, which influence the SELECT-list of the following SQL statement. The method get_calculation_info is called before the SQL statement is executed. The method calculate on the other hand is executed after the SQL statement has been executed. Here it is possible to calculate the value of the virtual elements, using the data retrieved from the database.
Hi Dominik, thanks for comments. I have updated where I misunderstood concept about 2 methods.