Application Development 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: 

How to create dynamic internal table with header line

Former Member
0 Kudos

Hello Experts,

I have a requirement where I need to get data for 10-12 info types using rp-provide-from-last macro. So I thought of using the subroutine instead of writting the same code again and again. But in order to do that I need to create an dynamic internal table with header line(Macro only accepts internal table with header line).

Infotypes used in the programs are

0000 Infotype 0000 (Actions)

0001 Infotype 0001 (Org. Assignment)

0002 Infotype 0002 (Personal Data)

0006 Infotype 0006 (Addresses)

0008 Infotype 0008 (Basic Pay)

0009 Infotype 0009 (Bank Details)

0014 Infotype 0014 (Recur. Payments/Deds.)

0015 Infotype 0015 (Additional Payments)

0016 Infotype 0016 (Contract Elements)

0021 Infotype 0021 (Family)

0022 Infotype 0022 (Education)

0105 Infotype 0105 (Communications)

0185 Infotype 0185 [Identification] (SG)

0465 Infotype 0465

Code Snippet:

CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      tabname        = 'PS0000'
    TABLES
      dfies_tab      = t_dfies
    EXCEPTIONS
      not_found      = 1
      internal_error = 2
      OTHERS         = 3.

  rp-provide-from-last p0000 space f_date f_date.
  LOOP AT t_dfies.
    ASSIGN COMPONENT t_dfies-fieldname OF STRUCTURE p0000  TO <l_fs>.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.
    wa_infotypedata-fieldname =  t_dfies-fieldname.
    wa_infotypedata-fieldvalue = <l_fs>.
    APPEND wa_infotypedata TO t_infotypedata.

  ENDLOOP.
  wa_infotypestr-infotype = '0000'.
  wa_infotypestr-infotypedata = t_infotypedata.
  APPEND wa_infotypestr TO t_infotypestr.

<Fomatted the code snippet>

Please advice how I can do it.

Thanks In Advance.

Gaurav Mittal

Moderator Message: Please use tags to format your code snippet.

Edited by: Suhas Saha on Feb 3, 2012 10:33 AM

4 REPLIES 4

kesavadas_thekkillath
Active Contributor
0 Kudos

What is the error & where is the error ?

0 Kudos

Hi Keshav,

I need to pass the internal table with header line to the Macro "rp-provide-from-last p0000 space f_date f_date". here instead of P0000 I want to pass the table dynamically.

On this statement syntax error is coming that "Internal Table with header line is required".

Thanks,

Gaurav Mittal

Former Member
0 Kudos

Hi,

For dynamic internal table if you want to create.. This is the following code which I used in my program.

You can use this as a reference



PARAMETERS : p_table(10) TYPE C.

  DATA: w_tabname TYPE w_tabname,
        w_dref TYPE REF TO data,
        w_grid TYPE REF TO cl_gui_alv_grid.
  FIELD-SYMBOLS: <t_itab> TYPE ANY TABLE.
  w_tabname = p_table.
  CREATE DATA w_dref TYPE TABLE OF (w_tabname).
  ASSIGN w_dref->* TO <t_itab>.

* Populating Dynamic internal table

  SELECT * FROM (w_tabname) UP TO 20 ROWS INTO TABLE <t_itab>.

* Displaying dynamic internal table using Grid.

  CREATE OBJECT w_grid
    EXPORTING i_parent = cl_gui_container=>screen0.

  CALL METHOD w_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = w_tabname
    CHANGING
      it_outtab        = <t_itab>.
  CALL SCREEN 100.

TYPE-POOLS: slis.

FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE,   "Dynamic internal table name
              <fs_dyntable>,                      "Field symbol to create work area
              <fs_fldval> type any.  "Field symbol to assign values

PARAMETERS: p_cols(5) TYPE c.
"Input number of columns

DATA:   t_newtable TYPE REF TO data,
t_newline  TYPE REF TO data,
*t_fldcat   TYPE slis_t_fldcat_alv,
t_fldcat   TYPE lvc_t_fcat,
wa_it_fldcat TYPE lvc_s_fcat,
wa_colno(2) TYPE n,
wa_flname(5) TYPE c.

* Create fields .

DO p_cols TIMES.
  CLEAR wa_it_fldcat.
  move sy-index to wa_colno.
  concatenate 'COL'
              wa_colno
         into wa_flname.

  wa_it_fldcat-fieldname = wa_flname.
  wa_it_fldcat-datatype = 'CHAR'.
  wa_it_fldcat-intlen = 10.
  APPEND wa_it_fldcat TO t_fldcat.

ENDDO.

* Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = t_fldcat
  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

DATA: fieldname(20) TYPE c.
DATA: fieldvalue(10) TYPE c.
DATA: index(3) TYPE c.
DO p_cols TIMES.
  index = sy-index.
  MOVE sy-index TO wa_colno.
  CONCATENATE 'COL'
              wa_colno
         INTO wa_flname.

* Set up fieldvalue

  CONCATENATE 'VALUE' index INTO
              fieldvalue.
  CONDENSE    fieldvalue NO-GAPS.
  ASSIGN COMPONENT  wa_flname
      OF STRUCTURE <fs_dyntable> TO <fs_fldval>.
  <fs_fldval> =  fieldvalue.
ENDDO.

* Append to the dynamic internal table

APPEND <fs_dyntable> TO <t_dyntable>.
* Displaying dynamic internal table using Grid.
DATA: wa_cat type slis_fieldcat_alv,
      fs_fldcat type slis_t_fieldcat_alv.

DO p_cols TIMES.
  CLEAR wa_cat.
  MOVE sy-index TO wa_colno.
  CONCATENATE 'COL'
              wa_colno
         INTO wa_flname.
  wa_cat-fieldname = wa_flname.
  wa_cat-seltext_s = wa_flname.
  wa_cat-outputlen = '10'.
  APPEND wa_cat TO fs_fldcat.

ENDDO.

* Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    it_fieldcat = fs_fldcat
  TABLES
    t_outtab    = <t_dyntable>.

Hope this will be helpful to you.

Regards,

Jhings

Former Member
0 Kudos

Hi,

Go through this code,

type-pools : slis.

PARAMETERS : p_tab type dd02l-tabname.

data : ref_tabletype type REF TO cl_abap_tabledescr,

ref_rowtype TYPE REF TO cl_abap_structdescr.

field-symbols : <lt_table> type standard TABLE ,

<fwa> type any,

<field> type abap_compdescr.

data : lt_fcat type lvc_t_fcat,

ls_fcat type lvc_s_fcat,

lt_fldcat type SLIS_T_FIELDCAT_ALV,

ls_fldcat like line of lt_fldcat.

data : ref_data type REF TO data,

ref_wa type ref to data.

ref_rowtype ?= cl_abap_typedescr=>DESCRIBE_BY_name( p_name = p_tab ).

TRY.

CALL METHOD cl_abap_tabledescr=>create

EXPORTING

p_line_type = ref_rowtype

receiving

p_result = ref_tabletype.

CATCH cx_sy_table_creation .

write : / 'Object Not Found'.

ENDTRY.

*creating object.

create data ref_data type handle ref_tabletype.

create data ref_wa type handle ref_rowtype.

*value assignment.

ASSIGN ref_data->* to <lt_table>.

assign ref_wa->* to <fwa>.

loop at ref_rowtype->components ASSIGNING <field>.

ls_fcat-fieldname = <field>-name.

ls_fcat-ref_table = p_tab.

append ls_fcat to lt_fcat.

if lt_fldcat[] is INITIAL.

ls_fldcat-fieldname = 'CHECKBOX'.

ls_fldcat-checkbox = 'X'.

ls_fldcat-edit = 'X'.

ls_fldcat-seltext_m = 'Checkbox'.

append ls_fldcat to lt_fldcat.

endif.

clear ls_fldcat.

ls_fldcat-fieldname = <field>-name.

ls_fldcat-ref_tabname = p_tab.

append ls_fldcat to lt_fldcat.

endloop.

loop at lt_fldcat into ls_fldcat.

if sy-tabix = 1.

ls_fldcat-checkbox = 'X'.

ls_fldcat-edit = 'X'.

modify lt_fldcat FROM ls_fldcat

TRANSPORTING checkbox edit.

endif.

endloop.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt_fcat[]

IMPORTING

ep_table = ref_data.

assign ref_data->* to <lt_table>.

select * FROM (p_tab) into table <lt_table>.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IT_FIELDCAT = lt_fldcat[]

TABLES

t_outtab = <lt_table>.

Regards,

Aditya