Home > Default > How to fix a bug in Rich Heilman's weblog code?

How to fix a bug in Rich Heilman's weblog code?

November 30Hits:0
Advertisement
Dear all,
I'm trying to create a report on a code given in the weblog by Rich Heilman:
/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap
Unfortunately, there is a bug in the code and a short dump occurs:
A RAISE statement in the program "CL_ABAP_TYPEDESCR=============CP" raised the
exception condition "TYPE_NOT_FOUND".
I copied here the whole code:
REPORT  Z_DYNAMIC.
type-pools : abap.
field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
data: dy_table type ref to data,
      dy_line  type ref to data,
      xfc type lvc_s_fcat,
      ifc type lvc_t_fcat.
selection-screen begin of block b1 with frame.
parameters: p_table(30) type c default 'T001'.
selection-screen end of block b1.
start-of-selection.
  perform get_structure.
  perform create_dynamic_itab.
  perform get_data.
  perform write_out.
form get_structure.
data : idetails type abap_compdescr_tab,
       xdetails type abap_compdescr.
data : ref_table_des type ref to cl_abap_structdescr.
* Get the structure of the table.
  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( p_table ).
  idetails[] = ref_table_des->components[].
  loop at idetails into xdetails.
    clear xfc.
    xfc-fieldname = xdetails-name .
    xfc-datatype = xdetails-type_kind.
    xfc-intlen = xdetails-length.
    xfc-decimals = xdetails-decimals.
    append xfc to ifc.
  endloop.
endform.
form create_dynamic_itab.
* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=>create_dynamic_table
               exporting
                  it_fieldcatalog = ifc
               importing
                  ep_table        = dy_table.
  assign dy_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
  create data dy_line like line of <dyn_table>.
  assign dy_line->* to <dyn_wa>.
endform.
form get_data.
* Select Data from table.
  select * into table <dyn_table>
             from (p_table).
endform.
form write_out.
* Write out data from table.
  loop at <dyn_table> into <dyn_wa>.
    do.
      assign component  sy-index
         of structure <dyn_wa> to <dyn_field>.
      if sy-subrc <> 0.
        exit.
      endif.
      if sy-index = 1.
        write:/ <dyn_field>.
      else.
        write: <dyn_field>.
      endif.
    enddo.
  endloop.
endform.
Could someone fix a bug, please?
Best regards,
Eugene

Answers

Hi Eugene,
I made a small change to the code and it works even for MARA. See the code below..
REPORT zsritest58 .
TYPE-POOLS : abap.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <dyn_field>.
DATA: dy_table TYPE REF TO data,
      dy_line  TYPE REF TO data,
      xfc TYPE lvc_s_fcat,
      ifc TYPE lvc_t_fcat.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_table(30) TYPE c DEFAULT 'T001'.
SELECTION-SCREEN END OF BLOCK b1.
START-OF-SELECTION.
  PERFORM get_structure.
  PERFORM create_dynamic_itab.
  PERFORM get_data.
  PERFORM write_out.
*       FORM get_structure                                            *
FORM get_structure.
<b>*data : idetails type abap_compdescr_tab,
*       xdetails type abap_compdescr.
*data : ref_table_des type ref to cl_abap_structdescr.
** Get the structure of the table.
*  ref_table_des ?=
*      cl_abap_typedescr=>describe_by_name( p_table ).
*  idetails[] = ref_table_des->components[].
*  loop at idetails into xdetails.
*    clear xfc.
*    xfc-fieldname = xdetails-name .
*    xfc-datatype = xdetails-type_kind.
*    xfc-intlen = xdetails-length.
*    xfc-decimals = xdetails-decimals.
*    append xfc to ifc.
*  endloop.
  SELECT SINGLE tabname INTO p_table FROM dd02l
  WHERE tabname EQ p_table
    AND as4local EQ 'A'
    AND as4vers EQ '0000'.
  IF sy-subrc NE 0.
    STOP.
  ENDIF.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
   EXPORTING
*   I_BUFFER_ACTIVE              =
     i_structure_name             = p_table
*   I_CLIENT_NEVER_DISPLAY       = 'X'
*   I_BYPASSING_BUFFER           =
    CHANGING
      ct_fieldcat                  = ifc[]
   EXCEPTIONS
     inconsistent_interface       = 1
     program_error                = 2
     OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.</b>
ENDFORM.
*       FORM create_dynamic_itab                                      *
FORM create_dynamic_itab.
* Create dynamic internal table and assign to FS
  CALL METHOD cl_alv_table_create=>create_dynamic_table
               EXPORTING
                  it_fieldcatalog = ifc
               IMPORTING
                  ep_table        = dy_table.
  ASSIGN dy_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
  CREATE DATA dy_line LIKE LINE OF <dyn_table>.
  ASSIGN dy_line->* TO <dyn_wa>.
ENDFORM.
*       FORM get_data                                                 *
FORM get_data.
* Select Data from table.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table>
             FROM (p_table).
ENDFORM.
*       FORM write_out                                                *
FORM write_out.
* Write out data from table.
  LOOP AT <dyn_table> INTO <dyn_wa>.
    DO.
      ASSIGN COMPONENT  sy-index
         OF STRUCTURE <dyn_wa> TO <dyn_field>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF sy-index = 1.
        WRITE:/ <dyn_field>.
      ELSE.
        WRITE: <dyn_field>.
      ENDIF.
    ENDDO.
  ENDLOOP.
ENDFORM.
Hope this helps..
Sri

Read other 16 answers

Tags:

Related Articles

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 15 q. 0.931 s.