Fiori elements utilizing CDS with Virtual Elements
This Blog is about utilizing the CDS’s virtual elements feature which as exposed as ODATA Via SADL (direct in SEGW / with ODATA Publish true annotations). This ODATA can be further consumed by Fiori Elements based application.
(For more information on Fiori Elements check this link )
Also following SAP standard documents are helpful for understanding of CDS with Virtual elements , Link
Standard Consumption CDS is generating the Fiori elements application. There might be business requirement for field which required custom ABAP Logic for the calculation.
So good news is that we can use the CDS with virtual element and can write the complex custom Logic utilizing the ABAP Resource for the calculation.
Virtual elements can be implemented with Calculated value of Fields, Filtering of calculated values, Sorting of the Calculated value.
- Create the consumption CDS with UI and Virtual elements annotations.
- Give the Place holder for the Virtual elements, data type of the virtual elements must be defined.
- Create the normal ABAP class which implement the interface IF_SADL_EXIT_CALC_ELEMENT_READ.
- Implement both the methods GET_CALCULATE_INFO and CALCULATE.
- CDS can be exposed as ODATA or SEGW reference data source.
- Generate the Fiori elements-based application which show the calculated columns.
Let’s implement ABAP Code exit for Virtual elements.
Example – concatenate Material, Plant, Storage location separate by ‘-‘.
(This is just for demo purpose we can implement with CDS concat functions also)
- Create the CDS with Virtual element and with virtual elements specific annotations below
- We have given Place holder for the virtual element ‘TestExit’ with data type as abap.char(40)
- Create the class with implementing interface IF_SADL_EXIT_CALC_ELEMENT_READ.
- Implement the below method
- Method GET_CALCULATION_INFO is used to provide the list of fields which is required for the calculation.
- Method CALCULATE is actual method where we implement the custom logic.
- For the demo purpose, GET_CALCULATION_INFO is with blank implementation.
- Implement the CALCULATE method utilizing ABAP resource (i.e. Function Module etc)
- Register the Gateway Service generated by CDS and check the output in the Gateway Client
- We can see the calculated field in the response
- We can utilize the same CDS in Fiori Elements based application and Field with virtual element will be populated.
- Develop the Fiori Elements based List Report, for more information on Fiori element list refer following Link
CDS with virtual element is useful where the original data model doesn’t have the required field and needs to be calculated based on the custom logic using ABAP Resource (Function Module/ Class-Method etc) for the SADL based ODATA.
I did the exact same thing, but I faced an error.
In my case, Gateway and ABAP are two separate servers. When I call OData service from Gateway Client, for metadata it runs just fine whereas when I use entity set to display data, it throws an error as - "RFC Error: CREATE OBJECT: Class not found".
I believe, this error is due to the class we have used in CDS (@ObjectModel.virtualElementCalculatedBy: 'ABAP.ZCL_ZI_TESTABAPEXIT').
Thanks for trying the same and updating us with the results.
As I understand, You have created the Class and implements the required interfaces , then Ideally it should get trigger if the backend version is supported.
Could you please check the version of your backend system and check in SAP Help if it supports the CDS with Virtual elements .
Sorry for the delayed response.
The issue is resolved. Some issue with the Class activation, I reimplemented the interface methods and activated and It simply worked. Thanks for this wonderful blog.
I have tried this scenario following mentioned steps in blog and everything works perfect.
I have one query here,Is there any limitation virtual element annotation will works only for OData or other consumers like(ABAP SQL Query,Analytical Query).
Good to know you have successfully tried it and liked the same.
Yes, it works with SADL based ODATA and won't work with Analytic query etc.
Thank you for clarification
Very helpful.Thanks for sharing Mohit.
I have a question. Can we also apply a filter on this virtual field?
Its not available via CDS annotations. I am also not able to configure this from UI5 extensions.
Could you please let me know if you have some idea about this?
I have recently started exploring the ABAP restful programming model and it is very difficult for us outside SAP to get a coherent set of reference on the capabilities of Fiori Elements. so thx for sharing. now the question how to handle additional user requests for UI interaction because the BOPF model seems to be gone now in new model.
Thanks in advance for your reply.
Thanks Mohit - added to Fiori elements wiki!
Thanks brilliant post. 🙂
Nice blog on virtual elements using custom ABAP code exit on abap cds.
Now we can read long text using above concept which was earlier not possible(read_text function in CDS views).
My concern here is if we are implementing this class using interface( if_sadl_exit_filter_transform, if_sadl_exit_calc_element_read, if_sadl_exit_sorrt_transform), where do it get excuted as abap class is on application layer while cds artifacts executed on RAM.
If it get executed on application layer then there will be performance issue, not sure if we can use amdp there for the same as amdp will push code on DB.
It will be on application layer as it's the regular abap code. Only filter and sort will be executed at the CDS view level.
But it will not cause a big issue as by best practice, we will only fetch first 20 records or something by top & skip criteria and the element_read method will be executed only for those 20 records. But if it is consumed in custom ui5 app where the best practices are not followed and fetched thousands of records, obviously the performance will be heavy.
But again, if the logic that we implement in that element_read method is heavy, even for the 20 records it will be heavy. So better to write the code thinking performance in mind in those exits.
Also, it's better to use AMDP and table function combination but not every thing is possible with them, for example, you cannot use classes or FMs inside AMDP.
I will try both scenarios using class and amdp(will see if any work around), then take performance trace with plan viz and compare.
I am using Virtual element calculated by in a consumption view to do one calculation of DegreeDays in IS-U project. But I am getting an error like "Calculated elements are not allowed in analytical views".
HI Seth ,
Did you any solution for the calculated elements in analytical views .
virtual elements not working for analytical CDS . Is there any option to pull virtual column in analytical CDS.
Please Suggest any alternative.
Thank you for sharing this wonderful post !!
Excellent blog!! I have a use case to hide the 'exclude ranges'(not equal to', 'not starts with','not less than', etc) options from the @UI.selectionField annotation in CDS. Can it be achieved using this virtual element concept? I maintain the field as a virtual element and then have the SELECT_OPTIONS_RESTRICT FM inside the class to hide the not relevant options?
First of all, thank you very much for the article. I have a question.
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