Read FI document from remote system with different coding block fields

The function FI_DOCUMENT_READ is a nice, standard, remote enabled function to read FI document to the BKPF and BSEG structures (internal tables). But what to do if there are differences in coding block of local and remote system? (when you created your own account assignment ZZ or YY fields in BSEG) In such case the local BSEG definition doesn’t fit to the remote one. One option is to create in local system the definition of remote BSEG structure, in dictionary or directly in program. It is quite uncomfortable, because BSEG has many fields and you have to maintain this definition when the remote coding block change.

Below a dynamic solution. It uses other remote enabled function RFC_GET_STRUCTURE_DEFINITION to read the definition of BSEG of other system. The local type is created dynamically with Run Time Type Creation (RTTC).

REPORT zs_remote_type.

PARAMETERS:
  pa_rfc   TYPE rfcdest OBLIGATORY, " RFC connection to remote system
  pa_bukrs TYPE bukrs   OBLIGATORY, " document key from remote system
  pa_belnr TYPE belnr_d OBLIGATORY, " document key from remote system
  pa_gjahr TYPE gjahr   OBLIGATORY. " document key from remote system

START-OF-SELECTION.

  PERFORM main.

*&---------------------------------------------------------------------*
*&      Form  main
*&---------------------------------------------------------------------*
FORM main.

  DATA:
    lr_struct_type TYPE REF TO cl_abap_structdescr,
    lr_struct_data TYPE REF TO data,
    lr_table_type  TYPE REF TO cl_abap_tabledescr,
    lr_table_data  TYPE REF TO data.

  FIELD-SYMBOLS:
    <fs_struct> TYPE any,
    <fs_table>  TYPE table.

  DATA:
    lt_bkpf TYPE TABLE OF bkpf,
    ls_bseg TYPE bseg,
    lt_bseg TYPE TABLE OF bseg.

* use Run Time Type Creation (RTTC) to create type for structure BSEG
* with fields from remote system
  PERFORM type_struct_create USING 'BSEG' pa_rfc CHANGING lr_struct_type.

  CREATE DATA lr_struct_data TYPE HANDLE lr_struct_type.
  ASSIGN lr_struct_data->* TO <fs_struct>.

* use Run Time Type Creation (RTTC) to create type for tabletype BSEG
* with fields from remote system
  lr_table_type = cl_abap_tabledescr=>create( lr_struct_type ).

  CREATE DATA lr_table_data TYPE HANDLE lr_table_type.
  ASSIGN lr_table_data->* TO <fs_table>.

* read document from remote system using remote BSEG definition
  CALL FUNCTION 'FI_DOCUMENT_READ'
    DESTINATION pa_rfc
    EXPORTING
      i_bukrs               = pa_bukrs
      i_belnr               = pa_belnr
      i_gjahr               = pa_gjahr
    TABLES
      t_bkpf                = lt_bkpf
      t_bseg                = <fs_table>
    EXCEPTIONS
      wrong_input           = 1
      not_found             = 2
      system_failure        = 10
      communication_failure = 11
      OTHERS                = 99.

  IF sy-subrc <> 0.
    WRITE: / 'Error, function FI_DOCUMENT_READ sy-subrc =', sy-subrc.
    STOP.
  ENDIF.

* copy fields to local BSEG definition
  LOOP AT <fs_table> ASSIGNING <fs_struct>.
    MOVE-CORRESPONDING <fs_struct> TO ls_bseg.
    WRITE: / ls_bseg-bukrs, ls_bseg-belnr, ls_bseg-gjahr, ls_bseg-buzei.
    APPEND ls_bseg TO lt_bseg.
  ENDLOOP.
ENDFORM.                    "main

*&---------------------------------------------------------------------*
*&      Form  type_struct_create
*&---------------------------------------------------------------------*
FORM type_struct_create USING    pi_tabname
                                 pi_rfcdest
                        CHANGING pr_struct_type TYPE REF TO cl_abap_structdescr.

  DATA:
    lv_uclen     TYPE i,
    lt_field     TYPE lvc_t_rfcfields,
    ls_field     TYPE rfc_fields,
    lt_component TYPE abap_component_tab,
    ls_component TYPE abap_componentdescr.

  lv_uclen = cl_abap_char_utilities=>charsize.

  CALL FUNCTION 'RFC_GET_STRUCTURE_DEFINITION'
    DESTINATION pi_rfcdest
    EXPORTING
      tabname               = pi_tabname
      uclen                 = lv_uclen
    TABLES
      fields                = lt_field
    EXCEPTIONS
      table_not_active      = 1
      system_failure        = 10
      communication_failure = 11
      OTHERS                = 99.

  IF sy-subrc <> 0.
    WRITE: / 'Error, function RFC_GET_STRUCTURE_DEFINITION sy-subrc =', sy-subrc.
    STOP.
  ENDIF.

  LOOP AT lt_field INTO ls_field.

*   internal datatypes (values of domain INTTYPE)
*
*   C - Character String
*   N - Character String with Digits Only
*   D - Date (Date: YYYYMMDD)
*   T - Time (Time: HHMMSS)
*   X - Byte Seq. (heXadecimal), in DDIC metadata also for INT1/2/4
*   I - Integer number (4-byte integer with sign)
*   b - 1-byte integer, integer number <= 254
*   s - 2-byte integer, only for length field before LCHR or LRAW
*   P - Packed number
*   F - Floating point number to accuracy of 8 bytes
*   g - Character string with variable length (ABAP type STRING)
*   y - Byte sequence with variable length (ABAP type XSTRING)
*   u - Structured type, flat
*   v - Structured type, deep
*   h - Table type
*   V - Character string (old Dictionary type VARC)
*   r - Reference to class/interface
*   l - Reference to data object
*   a - Decimal Floating Point Number, 16 Digits
*   e - Decimal Floating Point Number, 34 Digits
*   j - Static Boxed Components
*   k - Generic Boxed Components

    ls_component-name = ls_field-fieldname.

    CASE ls_field-exid.
      WHEN 'C'.
        ls_component-type = cl_abap_elemdescr=>get_c( ls_field-intlength ).
      WHEN 'D'.
        ls_component-type = cl_abap_elemdescr=>get_d( ).
      WHEN 'F'.
        ls_component-type = cl_abap_elemdescr=>get_f( ).
      WHEN 'g'.
        ls_component-type = cl_abap_elemdescr=>get_string( ).
      WHEN 'I'.
        ls_component-type = cl_abap_elemdescr=>get_i( ).
      WHEN 'N'.
        ls_component-type = cl_abap_elemdescr=>get_n( ls_field-intlength ).
      WHEN 'P'.
        ls_component-type = cl_abap_elemdescr=>get_p( p_length = ls_field-intlength p_decimals = ls_field-decimals ).
      WHEN 'T'.
        ls_component-type = cl_abap_elemdescr=>get_t( ).
      WHEN   'b' " byte
          OR 's' " short
          OR 'X'.
        ls_component-type = cl_abap_elemdescr=>get_x( ls_field-intlength ).
      WHEN 'y'.
        ls_component-type = cl_abap_elemdescr=>get_xstring( ).
      WHEN OTHERS.
        WRITE: / 'Error, field', ls_field-fieldname, '- unknow elementar type ', ls_field-exid.
        STOP.
    ENDCASE.

    APPEND ls_component TO lt_component.
  ENDLOOP.

  pr_struct_type = cl_abap_structdescr=>create( p_components = lt_component ).

ENDFORM.                    "type_struct_create

See other related notes on my website:

Scroll to Top