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: 

Dynamic Internal Table

Former Member
0 Kudos

DATA: LO_STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR.

DATA: LO_TABLE_TYPE TYPE REF TO CL_ABAP_TABLEDESCR.

DATA: LO_DATAREF TYPE REF TO DATA.

DATA: LT_COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

DATA: LS_COMP_TAB LIKE LINE OF LT_COMP_TAB.

FIELD-SYMBOLS: <LT_TABLE> TYPE STANDARD TABLE,

<LS_TABLE> TYPE ANY,

<LV_FIELD> TYPE ANY.

LS_COMP_TAB-NAME = P_1040.

LS_COMP_TAB-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( P_1041 ).

APPEND LS_COMP_TAB TO LT_COMP_TAB.

  • Create dynamic internal table based on LT_COMP_TAB

LO_STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( LT_COMP_TAB ).

LO_TABLE_TYPE = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LO_STRUCT_TYPE ).

  • Create references to dyn internal table and work area

CREATE DATA LO_DATAREF TYPE HANDLE LO_TABLE_TYPE.

ASSIGN LO_DATAREF->* TO <LT_TABLE>.

CREATE DATA LO_DATAREF TYPE HANDLE LO_STRUCT_TYPE.

ASSIGN LO_DATAREF->* TO <LS_TABLE>.

ASSIGN COMPONENT P_1655 OF STRUCTURE <LS_TABLE> TO <LV_FIELD>.

IF SY-SUBRC = 0.

<LV_FIELD> = P_DATA.

ENDIF.

APPEND <LS_TABLE> TO <LT_TABLE>.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = V_REPID

I_CALLBACK_USER_COMMAND = 'USER_COMMAND'

IS_LAYOUT = ZLAYOUT

IT_FIELDCAT = I_FIELDCAT

I_SAVE = 'A'

IT_EVENTS = V_EVENTS

TABLES

T_OUTTAB = <LT_TABLE>.

This is the code I have written for a Dynamic internal table.

Everything works fine.

Except at the ALV displey.

I am getting a Short Dump with the message 'Field symbol has not yet been assigned'.

Can someone help me with this please.

1 ACCEPTED SOLUTION

former_member188685
Active Contributor
0 Kudos

post the complete coding. so that it will be easy to check and fix the issue.

6 REPLIES 6

former_member188685
Active Contributor
0 Kudos

post the complete coding. so that it will be easy to check and fix the issue.

0 Kudos

DATA: LO_STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR.

DATA: LO_TABLE_TYPE TYPE REF TO CL_ABAP_TABLEDESCR.

DATA: LO_DATAREF TYPE REF TO DATA.

DATA: LT_COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.

DATA: LS_COMP_TAB LIKE LINE OF LT_COMP_TAB.

FIELD-SYMBOLS: <LT_TABLE> TYPE STANDARD TABLE,

<LS_TABLE> TYPE ANY,

<LV_FIELD> TYPE ANY.

LS_COMP_TAB-NAME = 'VBELN'.

LS_COMP_TAB-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'CHAR10' ).

APPEND LS_COMP_TAB TO LT_COMP_TAB.

Create dynamic internal table based on LT_COMP_TAB

LO_STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( LT_COMP_TAB ).

LO_TABLE_TYPE = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LO_STRUCT_TYPE ).

Create references to dyn internal table and work area

CREATE DATA LO_DATAREF TYPE HANDLE LO_TABLE_TYPE.

ASSIGN LO_DATAREF->* TO <LT_TABLE>.

CREATE DATA LO_DATAREF TYPE HANDLE LO_STRUCT_TYPE.

ASSIGN LO_DATAREF->* TO <LS_TABLE>.

ASSIGN COMPONENT 'VBELN' OF STRUCTURE <LS_TABLE> TO <LV_FIELD>.

IF SY-SUBRC = 0.

<LV_FIELD> = '10085'.

ENDIF.

APPEND <LS_TABLE> TO <LT_TABLE>.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = V_REPID

I_CALLBACK_USER_COMMAND = 'USER_COMMAND'

IS_LAYOUT = ZLAYOUT

IT_FIELDCAT = I_FIELDCAT

I_SAVE = 'A'

IT_EVENTS = V_EVENTS

TABLES

T_OUTTAB = <LT_TABLE>.

This is the complete code.

0 Kudos

How about trying the ALV OM instead of the function module.

DATA: lo_struct_type TYPE REF TO cl_abap_structdescr.
DATA: lo_table_type TYPE REF TO cl_abap_tabledescr.
DATA: lo_dataref TYPE REF TO data.

DATA: lt_comp_tab TYPE cl_abap_structdescr=>component_table.
DATA: ls_comp_tab LIKE LINE OF lt_comp_tab.

FIELD-SYMBOLS: <lt_table> TYPE STANDARD TABLE,
<ls_table> TYPE ANY,
<lv_field> TYPE ANY.

ls_comp_tab-name = 'VBELN'.
ls_comp_tab-type ?= cl_abap_datadescr=>describe_by_name( 'CHAR10' ).
APPEND ls_comp_tab TO lt_comp_tab.


*Create dynamic internal table based on LT_COMP_TAB
lo_struct_type = cl_abap_structdescr=>create( lt_comp_tab ).
lo_table_type = cl_abap_tabledescr=>create( p_line_type = lo_struct_type ).


*Create references to dyn internal table and work area
CREATE DATA lo_dataref TYPE HANDLE lo_table_type.
ASSIGN lo_dataref->* TO <lt_table>.
CREATE DATA lo_dataref TYPE HANDLE lo_struct_type.
ASSIGN lo_dataref->* TO <ls_table>.

ASSIGN COMPONENT 'VBELN' OF STRUCTURE <ls_table> TO <lv_field>.
IF sy-subrc = 0.
  <lv_field> = '10085'.
ENDIF.

APPEND <ls_table> TO <lt_table>.


DATA: lr_table TYPE REF TO cl_salv_table.
DATA: lr_functions TYPE REF TO cl_salv_functions.
DATA: lr_columns TYPE REF TO cl_salv_columns_table.
DATA: lr_column TYPE REF TO cl_salv_column_table.
TRY.
    cl_salv_table=>factory(
     IMPORTING
       r_salv_table   = lr_table
     CHANGING
       t_table        = <lt_table> ).
  CATCH cx_salv_msg .
ENDTRY.
lr_functions = lr_table->get_functions( ).
lr_functions->set_all( abap_true ).

lr_columns = lr_table->get_columns( ).
lr_column ?= lr_columns->get_column( 'VBELN' ).
lr_column->set_medium_text( 'Sales Document' ).

lr_table->display( ).

Regards,

Rich Heilman

0 Kudos

Modified code.

REPORT  Y_TEST.

type-pools:slis.
DATA: LO_STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR.
DATA: LO_TABLE_TYPE TYPE REF TO CL_ABAP_TABLEDESCR.
DATA: LO_DATAREF TYPE REF TO DATA.
data: wa_layout type SLIS_LAYOUT_ALV .
DATA: LT_COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE.
DATA: LS_COMP_TAB LIKE LINE OF LT_COMP_TAB.

data: it_fieldcat  TYPE  SLIS_T_FIELDCAT_ALV.
data: wa_fcat like line of it_fieldcat.
FIELD-SYMBOLS: <LT_TABLE> TYPE STANDARD TABLE,
<LS_TABLE> TYPE ANY,
<LV_FIELD> TYPE ANY.

LS_COMP_TAB-NAME = 'VBELN'.
LS_COMP_TAB-TYPE ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_NAME( 'CHAR10' ).
APPEND LS_COMP_TAB TO LT_COMP_TAB.

wa_fcat-fieldname = 'VBELN'.
wa_fcat-outputlen = 10.
wa_fcat-seltext_l = 'Sales Order'.
append wa_fcat to it_fieldcat.


*Create dynamic internal table based on LT_COMP_TAB
LO_STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( LT_COMP_TAB ).
LO_TABLE_TYPE = CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LO_STRUCT_TYPE ).


*Create references to dyn internal table and work area
CREATE DATA LO_DATAREF TYPE HANDLE LO_TABLE_TYPE.
ASSIGN LO_DATAREF->* TO <LT_TABLE>.
CREATE DATA LO_DATAREF TYPE HANDLE LO_STRUCT_TYPE.
ASSIGN LO_DATAREF->* TO <LS_TABLE>.

ASSIGN COMPONENT 'VBELN' OF STRUCTURE <LS_TABLE> TO <LV_FIELD>.
IF SY-SUBRC = 0.
<LV_FIELD> = '10085'.
ENDIF.

APPEND <LS_TABLE> TO <LT_TABLE>.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
IS_LAYOUT = WA_LAYOUT
IT_FIELDCAT = IT_FIELDCAT
I_SAVE = 'A'
*IT_EVENTS = V_EVENTS
TABLES
T_OUTTAB = <LT_TABLE>.

0 Kudos

Hello,

With this,

How will I be able to handle the User Commands function.

Thanks,

Archana.

former_member212653
Active Contributor
0 Kudos

check out this link...

http://help-abap.blogspot.com/2008/07/important.html

and try this code:



*&---------------------------------------------------------------------*
*& Report  ZDYNAMIC_ALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdynamic_alv.

FIELD-SYMBOLS:
<f_tab1> TYPE STANDARD TABLE.

PARAMETERS:
p_tname TYPE tabname16 OBLIGATORY, " DEFAULT 'MARA' ,
p_rows(5) TYPE c DEFAULT '200'.
*----------------------------------------------------------------------*
*       CLASS lcl_dynamic DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_dynamic DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
    check_selection
     EXCEPTIONS invalid_table,
    main
     EXCEPTIONS no_data_found,
    display.

  PRIVATE SECTION.

    TYPE-POOLS: abap.

    CLASS-DATA: tab TYPE REF TO cl_abap_structdescr,
          wa_tab TYPE REF TO cl_abap_structdescr,
          comp_tab   TYPE cl_abap_structdescr=>component_table,
          i_tab TYPE REF TO cl_abap_tabledescr,
          i_table TYPE REF TO data.
ENDCLASS.                    "lcl_dynamic DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_dynamic IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_dynamic IMPLEMENTATION.
  METHOD check_selection.

    SELECT COUNT( * )
      FROM dd02l
      WHERE tabname  = p_tname
      AND   as4local = 'A'
      AND   tabclass = 'TRANSP'.
    IF sy-subrc <> 0.
      RAISE invalid_table.
    ENDIF.

  ENDMETHOD.                    "check_selection
  METHOD main.
    tab ?= cl_abap_typedescr=>describe_by_name( p_tname ).
    comp_tab = tab->get_components( ).
    wa_tab  = cl_abap_structdescr=>create( comp_tab ).
    i_tab   = cl_abap_tabledescr=>create( wa_tab ).
    CREATE DATA i_table TYPE HANDLE i_tab.
    ASSIGN i_table->* TO <f_tab1>.
    IF p_rows IS INITIAL.
      p_rows = '50000'.
    ENDIF.

*Get data
    SELECT * FROM (p_tname)
    INTO TABLE <f_tab1>
    UP TO p_rows ROWS.

    IF sy-subrc <> 0.
      RAISE no_data_found.
    ENDIF.
  ENDMETHOD.                    "main
  METHOD display.
    SET TITLEBAR sy-title
    OF PROGRAM sy-cprog
    WITH 'Display table:' p_tname.

    DATA:
    l_gr_alv TYPE REF TO cl_salv_table,
    l_gr_functions TYPE REF TO cl_salv_functions.

    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = l_gr_alv
          CHANGING
            t_table      = <f_tab1>.
      CATCH cx_salv_msg .   "#EC NO_HANDLER
    ENDTRY.
    l_gr_functions = l_gr_alv->get_functions( ).
    l_gr_functions->set_all( abap_true ).
    l_gr_alv->display( ).
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_dynamic IMPLEMENTATION

AT SELECTION-SCREEN.
  CALL METHOD lcl_dynamic=>check_selection
    EXCEPTIONS
      invalid_table = 1.
  IF sy-subrc <> 0.
    MESSAGE e001(00) WITH
    p_tname ' is not a Transparant Table'.
  ENDIF.

START-OF-SELECTION.

  CALL METHOD lcl_dynamic=>main
    EXCEPTIONS
      no_data_found = 1.
  IF sy-subrc <> 0.
    MESSAGE i001(00) WITH 'No data found'.
    LEAVE LIST-PROCESSING.
  ENDIF.


END-OF-SELECTION.

  CALL METHOD lcl_dynamic=>display.