Monday 12 May 2014

Dynamic internal table creation : RTTS

Sometimes we need to create internal table dynamically at runtime.
Suppose there is a requirement like we have certain field need to populate based on some condition.
If I tell you more practically like we need to create internal table dynamically when a excel file will be uploaded and there would not be fixed fields (different fields can be uploaded in the excel file as per the requirement at different time).

The requirement can be like when we need to work with access sequence in SD.

Or the requirement would be like if you need to create internal table based on the selection screen input.

Here I have tried to bring a simplest program where you can put the fields as per your requirement in the selection screen and can form the internal table based on the selection screen input fields dynamically.

Steps:
  1. Get the field names from the selection screen input.
  2. Get the domain details from the DD03L table based on the field names.
  3. Prepare the fieldcatalog for next step.
  4. CALL METHOD cl_alv_table_create=>create_dynamic_table using the fieldcatalog.
  5. Get the <fs_data> from the above method and
  ASSIGN <fs_data>->* TO <fs_it>.
6.  Our dynamic internal table <fs_it> has been created.

So.. let’s go..


REPORT z_demo_rtts.

TABLES:  tkeig.

*** Tables
DATAit_data TYPE REF TO data.
DATAit_fieldcatalog TYPE lvc_t_fcat.

*** Structure
DATAwa_fieldcatalog TYPE lvc_s_fcat,
      wa_dd03l TYPE dd03l.

*** Data References
DATAnew_line TYPE REF TO data.

*** Field Symbols
FIELD-SYMBOLS<fs_data> TYPE REF TO data,
               <fs_it> TYPE ANY TABLE,
               <fs_wa>,
               <fs_wa_component>.

DATAv_field_count TYPE i,
      v_count       TYPE i.

SELECT-OPTIONSs_field FOR  tkeig-fieldname.

LOOP AT s_field.
  SELECT SINGLE FROM dd03l INTO wa_dd03l WHERE fieldname s_field-low.
  IF sy-subrc 0.
    wa_fieldcatalog-fieldname s_field-low.
    wa_fieldcatalog-inttype wa_dd03l-inttype.
    APPEND wa_fieldcatalog TO it_fieldcatalog.
  ENDIF.
ENDLOOP.

*wa_fieldcatalog-fieldname = 'CARRID'. "Fieldname
*wa_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
*APPEND wa_fieldcatalog TO it_fieldcatalog.


ASSIGN it_data TO <fs_data>.

CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog           it_fieldcatalog
  IMPORTING
    ep_table                  <fs_data>
  EXCEPTIONS
    generate_subpool_dir_full 1
    OTHERS                    2.
IF sy-subrc <> 0.
ENDIF.


*** So <fs_it> now points to our dynamic internal table.
ASSIGN <fs_data>->TO <fs_it>.

*** Next step is to create a work area for our dynamic internal table.
CREATE DATA new_line LIKE LINE OF <fs_it>.

*** A field-symbol to access that work area
ASSIGN new_line->*  TO <fs_wa>.

*** And to put the data in the internal table
SELECT *
  FROM vbap
  UP TO ROWS
  INTO CORRESPONDING FIELDS OF TABLE <fs_it>.

LOOP AT s_field.
  WRITEs_field-low.
ENDLOOP.

DESCRIBE TABLE s_field LINES v_field_count.

*** Access contents of internal table
LOOP AT <fs_it> ASSIGNING <fs_wa>.

  NEW-LINE.
  v_count 0.
  DO v_field_count TIMES.
  v_count v_count + 1.
  ASSIGN COMPONENT v_count OF STRUCTURE <fs_wa> TO <fs_wa_component>.
  WRITE<fs_wa_component>.
  WRITE'                  '.
  ENDDO.

*  ASSIGN COMPONENT 2 OF STRUCTURE <fs_wa> TO <fs_wa_component>.
*  WRITE: <fs_wa_component>.

ENDLOOP.


Selection screen input:


Output:

1 comment: