Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Filling Dynamic table with internal table field values

Former Member
0 Likes
1,952

I have an internal table, it_container, with two fields: element, value. I need to loop through this table and when it_container-element = (fieldname of dynamic table) to place it_container-value into that field of my dynamic table. But I don't know how to reference the fieldname of the dynamic table inside of this loop. Can anyone help me?

Thanks,

Dan

1 ACCEPTED SOLUTION
Read only

former_member194669
Active Contributor
0 Likes
1,229

Try something like this way


" Fisrt find all fields in dynamic internal table (say dynamic workarea)
i_ref_descr1 ?= cl_abap_tabledescr=>describe_by_data( <wa_dynamic> ). 
i_details1[] = i_ref_descr1->components[].

loop at <i_dynamic> assigning <wa_dynamic>.
  loop at i_details1.
    v_tabix = sy-tabix
    read table it_container into wa_container with key element = i_details1-name.
    if sy-subrc eq 0.
       assign component v_tabix of structure <wa_dynamic> to <fs>.
       write wa_container-value to <fs>. 
    endif. 
  endloop.
endloop.

a®

5 REPLIES 5
Read only

Former Member
0 Likes
1,229

Hi

This can be done easily by assigning a component of the structure to a variable or for comparing it.

Do an F1 on

FIELD-SYMBOLS

and look for sample code on how to assign a component structure..

This should solve your problem...

Cheers

Ravish

Read only

former_member194669
Active Contributor
0 Likes
1,230

Try something like this way


" Fisrt find all fields in dynamic internal table (say dynamic workarea)
i_ref_descr1 ?= cl_abap_tabledescr=>describe_by_data( <wa_dynamic> ). 
i_details1[] = i_ref_descr1->components[].

loop at <i_dynamic> assigning <wa_dynamic>.
  loop at i_details1.
    v_tabix = sy-tabix
    read table it_container into wa_container with key element = i_details1-name.
    if sy-subrc eq 0.
       assign component v_tabix of structure <wa_dynamic> to <fs>.
       write wa_container-value to <fs>. 
    endif. 
  endloop.
endloop.

a®

Read only

0 Likes
1,229

Maybe I should specify a little better. I am writing a program to allow user to input a Task ID. I search the SWWWIHEAD table for all work items that contain that task, and use SAP_WAPI_READ_CONTAINER to retrieve the name-value pairs in it_container. Because multiple work items will be pulled, the it_container-elements are not unique. I am basically looking for an easy way to break up it_container into individual work items that can be displayed as a single row in an ALV grid that has columns of the container elements. Also, not all container elements are returned for every work item. For example, Return and ReturnHeader may be returned for one work item, but not the next. To build the dynamic table I used a work area that I sorted and deleted ajacent duplicates, so the order of elements of it_container does not match the order of fields in the dynamic table.

Any other suggestions?

Thanks,

Dan

Read only

0 Likes
1,229

Here is the code I have right now, maybe this will help:

TYPE-POOLS: slis, vrm.

TABLES: swwwihead, swr_cont.

FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE,

<fs_dyntable>,

<fs_fldval> type any.

DATA: t_newtable TYPE REF TO data,

t_newline TYPE REF TO data,

fs_fldcat TYPE slis_t_fieldcat_alv,

t_fcat TYPE lvc_t_fcat,

wa_it_fldcat TYPE lvc_s_fcat,

wa_colno(2) TYPE n,

wa_flname(32) TYPE c,

wi_count TYPE i,

prevvalue(50) TYPE c,

index(6) TYPE c,

count TYPE n.

DATA: firstvalue(32) TYPE c.

DATA: name TYPE vrm_id,

list TYPE vrm_values,

value LIKE LINE OF list.

DATA: it_container type swr_cont occurs 0 with header line.

DATA: wa_container type swr_cont occurs 0 with header line.

DATA: begin of it_swwwihead occurs 0,

wi_id like swwwihead-wi_id,

end of it_swwwihead.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS: p_task LIKE swwwihead-wi_rh_task OBLIGATORY.

SELECT-OPTIONS: s_date FOR sy-datum OBLIGATORY.

SELECTION-SCREEN END OF BLOCK b1.

SELECT wi_id INTO TABLE it_swwwihead FROM swwwihead

WHERE wi_rh_task = p_task AND wi_cd IN s_date.

loop at it_swwwihead.

CALL FUNCTION 'SAP_WAPI_READ_CONTAINER'

EXPORTING

workitem_id = it_swwwihead-wi_id

TABLES

simple_container = it_container.

ENDLOOP.

CLEAR wa_container.

LOOP AT it_container.

append it_container-element to wa_container.

ENDLOOP.

  • This gets rid of element returning twice from one work item with

  • two different values

LOOP AT it_container.

count = 2.

IF prevvalue IS NOT INITIAL.

IF it_container-element = prevvalue.

index = sy-tabix.

CONCATENATE prevvalue count INTO it_container-element.

WRITE it_container-element TO wa_container INDEX index.

count = count - 1.

index = index - 1.

CONCATENATE prevvalue count INTO it_container-element.

WRITE it_container-element TO wa_container INDEX index.

ELSE.

prevvalue = it_container-element.

ENDIF.

ELSE.

prevvalue = it_container-element.

ENDIF.

ENDLOOP.

CLEAR index.

LOOP AT wa_container.

index = sy-tabix.

MODIFY it_container INDEX index FROM wa_container TRANSPORTING element.

ENDLOOP.

SORT wa_container BY element.

DELETE ADJACENT DUPLICATES FROM wa_container.

  • Create fields .

CLEAR wa_it_fldcat.

LOOP AT wa_container into wa_flname.

wa_it_fldcat-fieldname = wa_flname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = '32'.

APPEND wa_it_fldcat TO t_fcat.

ENDLOOP.

  • Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fcat

IMPORTING

ep_table = t_newtable.

ASSIGN t_newtable->* TO <t_dyntable>.

  • Create dynamic work area and assign to FS

CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

ASSIGN t_newline->* TO <fs_dyntable>.

  • Populating Dynamic internal table

  • Where I need help!!!

  • Append to the dynamic internal table

APPEND <fs_dyntable> TO <t_dyntable>.

  • Displaying dynamic internal table using Grid.

DATA: wa_cat LIKE LINE OF fs_fldcat.

CLEAR wa_cat.

LOOP AT wa_container.

wa_cat-fieldname = wa_container-element.

wa_cat-seltext_l = wa_container-element.

wa_cat-outputlen = '32'.

APPEND wa_cat TO fs_fldcat.

ENDLOOP.

  • Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

it_fieldcat = fs_fldcat

TABLES

t_outtab = <t_dyntable>.

I know it's full of wasteful loops and such, but right now I just want the report to work and will worry about performance and things later.

Thanks,

Dan

Read only

0 Likes
1,229

Check this

CREATE DATA t_newline LIKE LINE OF .


loop at it_container into wa_container.
  loop at t_fcat.  
  " For getting the field position in dyn int table,  and sy-tabix has been used in assign component statement
    concatenate 'WA_CONTAINER-' t_fcat-fieldname into v_fieldname.
    condense v_fieldname no-gaps.
    assign component sy-tabix of structure <fs_dyntable> to <fs>.
    write (v_fieldname) to <fs>.
  endloop.
  " After the full loop of t_fcat we need to append ie check for all fields of dyn table
  append <fs_dyntable> to <t_dyntable>.
endloop.

a®