Skip to Content
Technical Articles
Author's profile photo Mohit Bansal

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

Business Scenario

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.

Technical Explanation

  • 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.

 

Detailed explanation

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
  1. Method GET_CALCULATION_INFO is used to provide the list of fields which is required for the calculation.
  2. 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

Output

 

Summary

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.

Assigned Tags

      21 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Aman Bhardwaj
      Aman Bhardwaj

      Hello Mohit,

      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.

      BR,
      Aman

       

      Author's profile photo Mohit Bansal
      Mohit Bansal
      Blog Post Author

      Hi Aman,

      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 .

      Regards,

      Mohit

       

       

      Author's profile photo Aman Bhardwaj
      Aman Bhardwaj

      Dear Mohit,

      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.

      BR,
      Aman

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Mohit,

      Good 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).

       

      Thank you,

      Syam

      Author's profile photo Mohit Bansal
      Mohit Bansal
      Blog Post Author

      Hi Syam,

       

      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.

       

      Regards,

      Mohit Bansal

       

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Thank you for clarification

      Author's profile photo Aparna Kuchibhotla
      Aparna Kuchibhotla

      Very helpful.Thanks for sharing Mohit.

      Author's profile photo s nath
      s nath

      Hi 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?

       

      Regards,

      Soumalya

      Author's profile photo Former Member
      Former Member

      Dear Mohit,

      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.

       

      Thx,

      PSB

      Author's profile photo Jocelyn Dart
      Jocelyn Dart

      Thanks Mohit - added to Fiori elements wiki!

      Author's profile photo Pavan Golesar
      Pavan Golesar

      Thanks brilliant post.  🙂

       

      -Pavan Golesar

      Author's profile photo Abhijeet Kankani
      Abhijeet Kankani

      Hi Mohit,

      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.

      Abhijeet kankani

       

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      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.

      Thanks,
      Mahesh

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      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.

      Author's profile photo Abhijeet Kankani
      Abhijeet Kankani

      Thanks Mahesh!

      I will try both scenarios using class and amdp(will see if any work around), then take performance trace with plan viz and compare.

      Regards,

      Abhijeet Kankani

      Author's profile photo Seth Protus
      Seth Protus

      Hi Mohit,

      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".

      Please suggest.

      Seth.

      Author's profile photo Kameswara Gonugunta
      Kameswara Gonugunta

      HI Seth ,

       

      Did you any solution for the calculated elements in analytical views .

      Author's profile photo DurgaPrasanth vemula
      DurgaPrasanth vemula

      Hi,

      virtual elements not working for analytical CDS . Is there any option to pull virtual column in analytical  CDS.

      Please Suggest any alternative.

      Thanks

       

       

       

      Author's profile photo Devanshu Dubey
      Devanshu Dubey

      Hi Mohit,

      Thank you for sharing this wonderful post !!

      Author's profile photo Kajal .
      Kajal .

      Hi Mohit!

      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?

      Author's profile photo Orhan Er
      Orhan Er

      Hello Jan,

       

      First of all, thank you very much for the article. I have a question.
      How can we assign a parameter (in Javascript) in UI5 (not with Fiori) to this Virtual element. Because then I want to access the data assigned to this Virtual element from inside the Class.
      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

       

      Thank you.

      Orhan