Skip to Content
Technical Articles
Author's profile photo Siva rama Krishna Pabbraju

Read Long Texts using ‘READ_TEXT’ in CDS Views

As we all know that ‘READ_TEXT’ is a function module and cannot be used in CDS Views. Only Open SQL with certain capabilities can be used in CDS Views.

Recently I got a requirement to fetch Header Note of PO from an existing CDS View. I have looked for possibilities regarding this. We can use tables STXH and STXB to get data. But the value will be in LRAW. In ABAP CDS View we do not have option to convert LRAW to String. [ I think it may be possibly in Native SQL to use SQL Built in Functions  like TO_NVARCHAR, TO_BINARY, TO_BLOB…I am not sure though]

Then I have come to know about Virtual Elements with CDS. With Virtual elements we can create a virtual element in CDS View and can have an enhancement attached to it. We can then implement the enhancement to fill the virtual element.

In My case I would need  add a virtual element in CDS View and implement an enhancement to fill the virtual element( to get Header Note).

I have added Virtual element here and mentioned implemented  class as “ZCL_MM_PO_HEADER_NOTE”.

Create enhancement Class with interface ‘IF_SADL_EXIT_CALC_ELEMENT_READ’ . Then implement method – IF_SADL_EXIT_CALC_ELEMENT_READ~CALCULATE

 

Fill out CT_CALCULATED_DATA table with which we need to fill values for Virtual element. In my case I will values with Header note.

 

Enhancement class will get triggered for each and every record that to be displayed while executing the CDS View.

 

Conclusion: We can implement any logic in CDS View that can be imeplemented in ABAP using Virtual elements concept.

 

References: https://blogs.sap.com/2020/01/13/using-virtual-elements-with-cds-in-fiori-elements/

 

Sample Code:

My CDS View:

@AbapCatalog.sqlViewAppendName: 'ZC_POFS_EXT'
@EndUserText.label: 'Extension for C_PurchaseOrderFs'
extend view C_PurchaseOrderFs with ZC_PurchaseOrderFs_Ext 
  association[0..1] to ZMM_PO_WF_CUST_FIELD as CustFields
  on CustFields.ebeln = purchaseorder
  {
  
@EndUserText: {label: 'Project Id'}
      @UI.identification: [{
             position: 30,
             label: 'Project Id', 
             importance: #HIGH }]
    CustFields.ProjectId as ProjectId,
    
    
        @EndUserText: {label: 'Project Description'}
      @UI.identification: [{
             position: 30,
             label: 'Project Description',
             importance: #HIGH }]
    CustFields.ProjectDescription as ProjectDescription,
    
    
    @EndUserText: {label: 'Header Note'}
    @ObjectModel.readOnly: true
    @ObjectModel.virtualElement: true
    @ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_MM_PO_HEADER_NOTE'
    
      @UI.identification: [{
             position: 40,
             label: 'Header Note',
             importance: #HIGH }]
    cast('' as abap.char(1333)) as HeaderNote  
    
}

 

Code in Method – Calculate:

  METHOD if_sadl_exit_calc_element_read~calculate.

    DATA: lv_ebeln  TYPE ebeln,
          lv_name   TYPE tdobname,

          lt_lines  TYPE STANDARD TABLE OF tline,
          lt_lines1 TYPE STANDARD TABLE OF tdline.

    LOOP AT it_original_data ASSIGNING FIELD-SYMBOL(<lfs_org_data>).

      DATA(lv_index) = sy-tabix.

      ASSIGN COMPONENT 'PURCHASEORDER' OF STRUCTURE <lfs_org_data> TO FIELD-SYMBOL(<lfs_ebeln>).
      IF <lfs_ebeln> IS ASSIGNED.
        lv_name = <lfs_ebeln>.

**--Read Header Note from
        CLEAR: lt_lines,lt_lines1.
        CALL FUNCTION 'READ_TEXT'
          EXPORTING
            client                  = sy-mandt
            id                      = 'F02'
            language                = 'E'
            name                    = lv_name
            object                  = 'EKKO'
          TABLES
            lines                   = lt_lines
          EXCEPTIONS
            id                      = 1
            language                = 2
            name                    = 3
            not_found               = 4
            object                  = 5
            reference_check         = 6
            wrong_access_to_archive = 7
            OTHERS                  = 8.

        IF NOT lt_lines IS INITIAL.

          lt_lines1  = VALUE #( FOR ls_lines IN lt_lines
                              ( ls_lines-tdline ) ).

          DATA(lv_string) = concat_lines_of( table = lt_lines1 sep = CL_ABAP_CHAR_UTILITIES=>newline ).

          READ TABLE ct_calculated_data ASSIGNING FIELD-SYMBOL(<lfs_cal_data>) INDEX lv_index.
          IF sy-subrc = 0.
             <lfs_cal_data> = lv_string.
          ENDIF.
        ENDIF. "IF NOT lt_lines IS INITIAL.
      ENDIF. "IF <lfs_ebeln> IS ASSIGNED.
    ENDLOOP. "LOOP AT IT_ORIGINAL_DATA ASSIGNING FIELD-SYMBOL(<lfs_org_data>).

  ENDMETHOD.

Assigned tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Hello. Could you provide the code in text instead of screenshot please? Thanks a lot!

      Author's profile photo Michael Biber
      Michael Biber

      Nice one!

      But the first thing that came to my mind: that spoils the whole performance benefit of code push down. An ABAP routine that needs to be called for every record which then calls a function module... Technically possible but is it really fast?

      Author's profile photo Sandra Rossi
      Sandra Rossi

      By the way, to not impair the performance, there was an alternative solution proposed in this blog post (that requires to create a program which daily runs to index the text table into a classic table) : https://blogs.sap.com/2019/12/02/standard-text-objects-search-using-cds-views/

      Author's profile photo Siva rama Krishna Pabbraju
      Siva rama Krishna Pabbraju
      Blog Post Author

      Agree, For performance point of view my approach is not suitable. Mostly when once is performing search and filter functions on this CDS View.

      But my case was different, I have only record at a time. So it works fine for me.

      Thanks for mentioning below URL here.

      https://blogs.sap.com/2019/12/02/standard-text-objects-search-using-cds-views/

      Author's profile photo Jan Braendgaard Petersen
      Jan Braendgaard Petersen

      Just to clarify, the ABAP method is called only once after data is fetched. It then has access to the whole dataset.

      But yes, performance-wise it's the opposite of code push down 🙂

      Author's profile photo Shai Sinai
      Shai Sinai

      There is also a standard FM for retrieval of multiple texts at once:

      FM READ_TEXT_TABLE (was introduced in note 2261311)

      Author's profile photo Vidar Gran Terjesen
      Vidar Gran Terjesen

      Running HANA 2.0.037, ERP on 1610 with Eclipse 2020-06 I don't get virutalElement accepted in Eclipse. Instead it outlines the text red as an unsupported feature, and it is not in the drop down list of possible annotations. Is there any way I still can use the virtualElement feature, or do someone know what  version is required?

      Author's profile photo Kameswara Rao Gonugunta
      Kameswara Rao Gonugunta

      hi

      virtual elements not working if aggregation is used in cds . do you have any solution for this please

      Author's profile photo Pramod Repaka
      Pramod Repaka

      Hi Siva,

       

      Thanks for the blog. But I am facing an issue "Could not display data. Contact your system administrator." on the PO item when I click the PO.

      This only happens when I have the

      @ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_MM_PO_HEADER_NOTE'

      But I am able to populate other fields in the extension. Only the virtual element seems to be not working.

      Can you please let me know how to resolve this.

       

      Thanks,

      Pramod

      Author's profile photo Pramod Repaka
      Pramod Repaka

      I got this resolved by not providing 'ABAP:' in the virtualElementCalculatedBy. It might help for someone.

      Author's profile photo Johannes Kretschmer
      Johannes Kretschmer

      Hi  Experts,

       

      so Virtual Elements is read only. What if i want to modify the text and want to use "SAVE_TEXT" .

      Or does SAP offers an other solution to work with text and cds views ?