ABAP Quick - Read table with RFC
Reading in a table via RFC function module is very easy, if you create a specific module for it. Reading any table can be just as easy.
Table of contents
It can sometimes be necessary to read data from another system via distributed landscapes or you want to distribute data from production to the other systems (test, development, etc.). There are several scenarios in which you can save your own function modules, as there are already generic variants for reading tables.
Advantages
The advantages of using a generic function modules are obvious:
- Time and cost savings through no own programming
- A standard check against the table permissions that protects sensitive data in the system
- Limitation of data and quantities in Select
RFC_READ_TABLE
The classic RFC function module is the RFC_READ_TABLE, since it covers most scenarios in most cases. It has an authorization check, the output fields can be changed in number and sequence. The amount of data returned is 512 characters, minus the column separators needed.
There are many examples of the use of this function module on the Internet, so we will only give a rough idea of its use here. We would like to share with you a qucik tip for creating the Where-Clause table.
DATA:
lt_opt TYPE STANDARD TABLE OF rfc_db_opt,
ls_where TYPE rsds_where.
" Create selection
IF mt_sel IS NOT INITIAL.
CALL FUNCTION 'AIBZ_FILL_WHERE_CLAUSE'
EXPORTING
i_tabname = md_table
it_select = mt_sel
IMPORTING
es_where_clause = ls_where.
lt_opt = ls_where-where_tab.
ENDIF.
After filling the selection with the individual restrictions, you can use the table to generate the where condition for the RFC function module. Also very handy when using dynamic selections.
Problems
As long as the width of the record is sufficient (512 characters), this is not a problem. But imagine you want to read a table where a column already has 512 characters or more. This is not possible with the function module and only generates an error while reading the data.
In our example, we would like to read IDoc data and verify the content. But the data field SDATA is 1000 characters wide and can not be read with the normal RFC function module. Here we reach the limit of this function module.
TABLE_ENTRIES_GET_VIA_RFC
The function module also checks the authorizations when reading the table. The line width is 2048 characters and no separators are used to return the data. However, the complete structure is always returned and you can not restrict the columns.
In addition, the function module has a problem with the conversion of number fields, which ends in a program exception. Therefore, we can only recommend the block conditionally.
CRM_CODEX_GET_TABLE_VIA_RFC
It roughly looks like a copy of the function module TABLE_ENTRIES_GET_VIA_RFC, but has been extended with crucial functions. Thus, an offset conversion is performed before the data allocation, which numbers are no longer a problem in the transfer.
For the above example, we have therefore once created a sample code, which also includes the subsequent conversion of the data.
DATA:
lt_sel TYPE STANDARD TABLE OF bdsel_stat,
lt_fields TYPE STANDARD TABLE OF bdi_mfgrp,
lt_data TYPE STANDARD TABLE OF bdi_entry,
ld_subrc TYPE sysubrc,
ld_sdata TYPE edi_sdata.
FIELD-SYMBOLS:
<ls_data> TYPE edid4,
<ls_head> TYPE e1fikpf.
" Create selection
APPEND VALUE #( zeile = |DOCNUM EQ '0000000009991234' AND SEGNAM EQ 'E1FIKPF'| ) TO lt_sel.
" Read entries from the table
CALL FUNCTION 'CRM_CODEX_GET_TABLE_VIA_RFC'
EXPORTING
tabname = 'EDID4'
IMPORTING
rc = ld_subrc
TABLES
sel_tab = lt_sel
nametab = lt_fields
tabentry = lt_data
EXCEPTIONS
internal_error = 1
table_has_no_fields = 2
table_not_activ = 3
not_authorized = 4
OTHERS = 5.
IF sy-subrc <> 0.
ENDIF.
" Map line
TRY.
" Read the first line
DATA(ld_line) = lt_data[ 1 ]-entry.
DATA(ls_field) = lt_fields[ fieldname = 'SDATA' ].
" Map field to structure
ld_sdata = ld_line ls_field-offset.
ASSIGN ld_sdata TO <ls_head> CASTING.
CATCH cx_root.
ENDTRY.
We read the header for a FIDCC1 or FIDCC2 and map the line to the data structure. This allows us to check the content of the header and, for example, validate the company code. This works just as well for other data and tables.
Conclusion
Reading generic data across systems should not be a problem for you with our little tip. In most cases, use the function module RFC_READ_TABLE because it has the most flexibility. If the requested data line is too big, you can now switch to other function modules. If all else fails, individual development remains for you.