3
votes

How do we concatenate fields of a dynamic work area? The idea is in the below code:

LOOP AT lt_final INTO DATA(ls_final).
  CONCATENATE ls_final-field1
              ls_final-field2
              ls_final-field3
              ls_final-field4
              ls_final-field5
         INTO ls_attachment SEPARATED BY lc_tab.   "lc_tab is horizontal tab

  APPEND ls_attachment TO lt_attachment.
  CLEAR: ls_attachment.
ENDLOOP.

(This code will be used for sending email attachment.) Now, my problem is, the internal table in the above code is a dynamic internal table, therefore I am not sure how many fields will be there and the field names as well.

How do I concatenate the fields? Any idea, please help..

LOOP AT <dynamic_table> INTO DATA(ls_final).
  CONCATENATE ls_final-(?)
              ls_final-(?)
              ls_final-(?)
              ls_final-(?)
              ls_final-(?) 
              "or more fields insert here depending on dynamic table
         INTO ls_attachment SEPARATED BY lc_tab.   "lc_tab is horizontal tab

  APPEND ls_attachment TO lt_attachment.
  CLEAR: ls_attachment.
ENDLOOP.
3

3 Answers

5
votes
FIELD-SYMBOLS: <lv_field> TYPE ANY.

LOOP AT lt_final
     ASSIGNING FIELD-SYMBOL(<ls_final>).
  DO.
    ASSIGN COMPONENT sy-index
           OF STRUCTURE <ls_final>
           TO <lv_field>.
    IF sy-subrc EQ 0.
      IF sy-index EQ 1.
        ls_attachment = <lv_field>.
      ELSE.
        ls_attachment = ls_attachment && lc_tab && <lv_field>.
      ENDIF.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.
ENDLOOP.

I hope it is self explaining, but: You can use the system variable (sy-index), it is incremented automatically by SAP. In the first step, just copy the value, there is nothing to concatenate yet (otherwise there will be an unnecessary lc_tab at the beginning of the string).

3
votes

Just read your structure by index.

data :
     lv_attachment type string.
     lv_index type i value 1.
field-symbols:
             <lv_value> type any.

while 1 = 1.
    assign component lv_index of structure ls_final to <lv_value>.
    if sy-subrc <> 0.
      exit.
    endif.
    concatenate lv_attachment <lv_value> into lv_attachment separated by lc_tab.
    lv_index = lv_index + 1.
endwhile.

Hope it helps.

2
votes

You can use CL_ABAP_CONTAINER_UTILITIES class for that task, method FILL_CONTAINER_C.

Here is the sample of populating dynamic table and concatenating its fields into container field:

PARAMETERS: p_tab TYPE string.

FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE.

DATA tab TYPE REF TO data.
CREATE DATA tab TYPE TABLE OF (p_tab).
ASSIGN tab->* TO <fs_tab>.

SELECT * UP TO 100 ROWS
  INTO TABLE <fs_tab>
  FROM (p_tab).

LOOP AT <fs_tab> ASSIGNING FIELD-SYMBOL(<fs_line>).

CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
  EXPORTING
    IM_VALUE                = <fs_line>
  IMPORTING
    EX_CONTAINER            = DATA(container)
  EXCEPTIONS
    ILLEGAL_PARAMETER_TYPE = 1
    others                 = 2.

CONDENSE container.

" do smth

ENDLOOP.