Skip to Content
Technical Articles

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.

10 Comments
You must be Logged on to comment or reply to a post.
  • 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

     

    • 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

       

       

      • 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

  • 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

    • 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

       

  • 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

  • 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