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.

Original Article:
https://blogs.sap.com/2020/04/07/read-long-texts-using-read_text-in-cds-views/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here