How to Read SAP Tables with a Generic ABAP Approach from UiPath

A longer time ago I published here a solution how to use the remote enabled function module RFC_READ_TABLE with the Invoke Code activity in the SAP back-end. Here now a solution to the same with an ABAP report, which works directly in the context of the UiPath automation workflow.

The first question that can be asked here is, why should I go into this trouble when I have a remote enabled function module available?
Well, this approach offers a few advantages:

  • The output size is 1024 characters, not 512 like in the RFM.
  • The content of the fields is condensed, leading and tailing blanks are removed.
  • Sorting of the Open SQL select statement is possible.
  • Non-invasive approach.

No light without shadow, the disadvantages:

  • Maximum 60.000 rows output.
  • This approach can only be used in testing and quality assurance systems.

To use this approach install ABAPRunner from the UiPath Marketplace.

Store the following ABAP code into your project directory.

"-Begin-----------------------------------------------------------------
"-
"-  Author: Stefan Schnell
"-  Version: 1.00
"-  Date: 2021-06-08
"-
"- Checked with SAP 7.50 SP 19 - Different tables
"- Checked with SAP 7.02 SP  6 - Different tables
"-
"-----------------------------------------------------------------------
REPORT z_rfc_read_table LINE-SIZE 1023.



"-Importing-------------------------------------------------------------
DATA:
  iv_query_table(30) TYPE C,
  iv_fields TYPE string VALUE '*',
  iv_where TYPE string VALUE SPACE,
  iv_sort TYPE string VALUE SPACE,
  iv_delimiter(1) TYPE C VALUE ';',
  iv_rowcount TYPE I VALUE 0,
  iv_rowskips TYPE I VALUE 0.

"-Exporting-------------------------------------------------------------
DATA:
  et_data TYPE STANDARD TABLE OF string.

"-Set import parameters-------------------------------------------------
"iv_query_table
"iv_fields
"iv_where
"iv_sort
"iv_delimiter
"iv_rowcount
"iv_rowskips



DATA:
  lr_data TYPE REF TO data,
  lr_ex TYPE REF TO cx_root,
  lo_table TYPE REF TO cl_abap_tabledescr,
  lo_struct TYPE REF TO cl_abap_structdescr,
  lt_comp TYPE abap_component_tab,
  ls_comp TYPE abap_componentdescr,
  lv_field_type(1) TYPE C,
  lv_data TYPE CHAR1024,
  lv_length TYPE i,
  val TYPE string,
  lt_fields TYPE STANDARD TABLE OF string,
  lv_field TYPE string.

FIELD-SYMBOLS:
  <lt_table> TYPE STANDARD TABLE,
  <wa> TYPE any,
  <val> TYPE any.



"-Check input-----------------------------------------------------------
IF iv_query_table IS INITIAL.
  WRITE: / 'Table name missing'.
  EXIT.
ENDIF.

IF iv_rowcount < 0.
  iv_rowcount = 0.
ENDIF.

IF iv_rowskips < 0.
  iv_rowskips = 0.
ELSEIF iv_rowskips > 0 AND iv_rowcount > 0.
  iv_rowcount = iv_rowcount + iv_rowskips.
ENDIF.

"-Create data object from table-----------------------------------------
TRY.
  CREATE DATA lr_data TYPE TABLE OF (iv_query_table).
  ASSIGN lr_data->* TO <lt_table>.
CATCH cx_sy_create_data_error INTO lr_ex.
  WRITE: / 'An exception occured'.
  EXIT.
ENDTRY.

"-Select data from table------------------------------------------------
SELECT (iv_fields)
  FROM (iv_query_table)
  INTO CORRESPONDING FIELDS OF TABLE <lt_table>
  UP TO iv_rowcount ROWS
  WHERE (iv_where)
  ORDER BY (iv_sort).
IF sy-subrc <> 0.
  WRITE: / 'No table data'.
  EXIT.
ENDIF.

"-Get field names-------------------------------------------------------
lo_table ?= cl_abap_typedescr=>describe_by_data( <lt_table> ).
lo_struct ?= lo_table->get_table_line_type( ).
lt_comp = lo_struct->get_components( ).

"-If field names specified, write it to table---------------------------
IF iv_fields <> '*'.
  SPLIT iv_fields AT SPACE INTO TABLE lt_fields.
ENDIF.

"-Get header data-------------------------------------------------------
IF lt_fields IS INITIAL.
  LOOP AT lt_comp INTO ls_comp.
    CONCATENATE lv_data ls_comp-NAME ';' INTO lv_data.
  ENDLOOP.
ELSE.
  "-If field names specified--------------------------------------------
  LOOP AT lt_comp INTO ls_comp.
    LOOP AT lt_fields INTO lv_field WHERE table_line = ls_comp-NAME.
      CONCATENATE lv_data ls_comp-NAME ';' INTO lv_data.
      EXIT.
    ENDLOOP.
  ENDLOOP.
ENDIF.

"-Delete last delimiter-------------------------------------------------
lv_length = strlen( lv_data ) - 1.
lv_data = lv_data(lv_length).

"-Append header data to export table------------------------------------
APPEND lv_data TO et_data.

"-Get table data--------------------------------------------------------
LOOP AT <lt_table> ASSIGNING <wa>.

  CHECK sy-tabix > iv_rowskips.

  CLEAR lv_data.

  DO.

    "-If field names specified------------------------------------------
    IF lt_fields IS NOT INITIAL.
      READ TABLE lt_comp INTO ls_comp INDEX sy-index.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      READ TABLE lt_fields WITH KEY = ls_comp-NAME
        TRANSPORTING NO FIELDS.
      CHECK sy-subrc = 0.
    ENDIF.

    "-Copy field value--------------------------------------------------
    ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <val>.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.
    "-Convert field value to string-------------------------------------
    val = <val>.
    "-Delete leading and trailing blanks--------------------------------
    CONDENSE val.
    "-Add value and delimter to row-------------------------------------
    CONCATENATE lv_data val iv_delimiter INTO lv_data.

  ENDDO.

  "-Delete last delimiter-----------------------------------------------
  lv_length = strlen( lv_data ) - 1.
  lv_data = lv_data(lv_length).

  "-Append table data to export table-----------------------------------
  APPEND lv_data TO et_data.

ENDLOOP.


"-Output export table---------------------------------------------------
LOOP AT et_data INTO lv_DATA.
  WRITE: / lv_DATA.
ENDLOOP.

"-End-------------------------------------------------------------------

Now we design the automation workflow. At first it is necessary to read the ABAP code. Now we replace in this example the comment line "iv_query_table with iv_query_table = ‘SFLIGHT’. Here you can use the table you need. In the next step we execute the ABAP report and write the result to a file. This file contains now the content of the table in CSV format. Last but not least this file is converted into a data table.

image

This is another interesting approach to read data from an SAP table and to work with it in the context of an UiPath automation workflow. The ABAP source code is directly available to us here, this offers further potential to us.

2 Likes

Great stuff @StefanSchnell ! New approach, new idea! I like it!

1 Like

Thank you very much @LevKushnir :slightly_smiling_face:

Really Awesome.

1 Like

Thank you very much @Manikandan_Ckj :slightly_smiling_face:

1 Like