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

ERROR IN CREATING DYNAMIC FIELDCATALOG

former_member574106
Participant
0 Likes
549
*&---------------------------------------------------------------------*
*& Report ZDYNAMICOOPS2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdynamicoops2.
INCLUDE ZDYNAMICOOPS2_top.
PERFORM fetch_data.
PERFORM build_table.
PERFORM build_fieldcatalog.
PERFORM alv_display.
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM fetch_data .

SELECT vbeln
       erdat
       ernam
       kunnr
       FROM vbak INTO TABLE it_vbak
       WHERE vkorg IN so_vkorg
       AND   erdat IN so_erdat.
*IF  ( so_erdat-high+4(2) - so_erdat-low+4(2) ) EQ '0 '.
*  EXIT.
*ENDIF.
LOOP AT it_vbak INTO wa_vbak.
l_month = wa_vbak-erdat+4(2).

* ASSIGNING months
CASE l_month.
WHEN '01'.
l_month_name = 'JAN'.
WHEN '02'.
l_month_name = 'FEB'.
WHEN '03'.
l_month_name = 'MAR'.
WHEN '04'.
l_month_name = 'APR'.
WHEN '05'.
l_month_name = 'MAY'.
WHEN '06'.
l_month_name = 'JUN'.
WHEN '07'.
l_month_name = 'JUL'.
WHEN '08'.
l_month_name = 'AUG'.
WHEN '09'.
l_month_name = 'SEP'.
WHEN '10'.
l_month_name = 'OCT'.
WHEN '11'.
l_month_name = 'NOV'.
WHEN '12'.
l_month_name = 'DEC'.
ENDCASE.
CLEAR : wa_vbak, l_month.
ENDLOOP.
*ENDIF.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_fieldcatalog
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM build_fieldcatalog .

* Create dynamic structure
  CREATE DATA gt_dyn_table TYPE (it_vbak).
  ASSIGN gt_dyn_table->* TO  <dyn_wa>.
* Fieldcatalog creation

   struct_descr ?= cl_abap_typedescr=>describe_by_data(
                  EXPORTING
                    p_data      = <dyn_wa> ).
*                  receiving
*                    p_descr_ref =



LOOP AT struct_descr->components ASSIGNING <dyn_comp>.
 wa_fldcat-fieldname = <dyn_comp>-name.
 wa_fldcat-ref_tabname = it_vbak.
 APPEND wa_fldcat TO it_fldcat.
 CLEAR  wa_fldcat.
 ENDLOOP.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form build_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM build_table .

DO l_month TIMES.
*IF l_month_name = l_month .
gw_dyn_fcat-fieldname = 'VBELN'.
gw_dyn_fcat-col_pos   = sy-index.
gw_dyn_fcat-outputlen = 10.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
CLEAR  gw_dyn_fcat.
*ENDIF.
gw_dyn_fcat-fieldname = 'L_MONTH_NAME'.
gw_dyn_fcat-col_pos   = sy-index.
gw_dyn_fcat-outputlen = 10.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
CLEAR  gw_dyn_fcat.
*ENDIF.
*gw_dyn_fcat-fieldname = 'ERNAM'.
*gw_dyn_fcat-col_pos   = sy-index.
*gw_dyn_fcat-outputlen = 10.
*APPEND gw_dyn_fcat TO gt_dyn_fcat.
*CLEAR  gw_dyn_fcat.
*ENDIF.

gw_dyn_fcat-fieldname = 'KUNNR'.               " Customer no is fixed "
gw_dyn_fcat-col_pos   = sy-index.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-fix_column = 'X'.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
CLEAR  gw_dyn_fcat.

ENDDO.

* Craeate dynamic table and assign it to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
*CALL METHOD zcl_alv_table_create_rttc=>create_dynamic_table(
  EXPORTING
*    i_style_table             =
    it_fieldcatalog           = gt_dyn_fcat
*    i_length_in_byte          =
  IMPORTING
    ep_table                  = gt_dyn_table
*    e_style_fname             =
  EXCEPTIONS
    generate_subpool_dir_full = 1
    OTHERS                    = 2
        .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ASSIGN gt_dyn_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
CREATE DATA gw_line LIKE LINE OF <dyn_table>.
ASSIGN gw_line->* TO <dyn_wa>.
* Append data to dynamic internal table.
LOOP AT it_vbak INTO wa_vbak.
ASSIGN COMPONENT 'VBELN' OF STRUCTURE <dyn_wa> TO <gv_field>.
IF sy-subrc = 0.
<gv_field> = wa_vbak-vbeln.
ENDIF.

ASSIGN COMPONENT 'KUNNR' OF STRUCTURE <dyn_wa> TO <gv_field>.
IF sy-subrc = 0.
<gv_field> = wa_vbak-kunnr.
ENDIF.

ASSIGN l_month_name TO <fs_month>.
*ASSIGN COMPONENT 'ERDAT' OF STRUCTURE <dyn_wa> TO <gv_field>.
*IF sy-subrc = 0.
*<gv_field> = wa_vbak-erdat.
*ENDIF.
*asd
APPEND <dyn_wa> TO <dyn_table>.
APPEND <fs_month> TO <dyn_table>.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form alv_display
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM alv_display .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
*   I_CALLBACK_PROGRAM                = ' '
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  =
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
   IT_FIELDCAT                       = gt_dyn_fcat
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
*   IT_SORT                           =
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
*   I_DEFAULT                         = 'X'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
*   IT_EVENTS                         =
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
*   O_PREVIOUS_SRAL_HANDLER           =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
  TABLES
    t_outtab                          = <dyn_table>
* EXCEPTIONS
*   PROGRAM_ERROR                     = 1
*   OTHERS                            = 2
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDFORM.

*&---------------------------------------------------------------------*
*& Include          ZDYNAMICOOPS2_TOP
*&---------------------------------------------------------------------*
TABLES : vbak.
TYPE-POOLS : slis.
DATA : var_date TYPE dats.
SELECT-OPTIONS : so_erdat FOR var_date,
                 so_vkorg FOR vbak-vkorg.

TYPES : BEGIN OF ty_vbak,
        vbeln TYPE vbak-vbeln,
        erdat TYPE vbak-erdat,
        ernam TYPE vbak-ernam,
        kunnr TYPE vbak-kunnr,
        END OF ty_vbak.


DATA : it_vbak TYPE TABLE OF ty_vbak WITH HEADER LINE,
       wa_vbak TYPE ty_vbak.
DATA : gt_dyn_table TYPE REF TO data,
       gw_line TYPE REF TO data,
       gw_line1 TYPE REF TO data,
       struct_descr TYPE REF TO cl_abap_structdescr.

* Fieldcatalog declarations

DATA : gt_dyn_fcat TYPE lvc_t_fcat,
       gw_dyn_fcat TYPE lvc_s_fcat,
       it_fldcat TYPE slis_t_fieldcat_alv,
       wa_fldcat TYPE slis_fieldcat_alv.

DATA : l_month TYPE vbak-erdat,
       l_month_name TYPE char3.
FIELD-SYMBOLS : <dyn_table> TYPE STANDARD TABLE,
                <dyn_wa> TYPE any,
                <gv_field> TYPE any,
                <dyn_comp> TYPE abap_compdescr,
                <fs_month> TYPE any.

Hi friends,

I am making my first dynamic field catalog. In this scenario, I need to enter any random date such as 01.01.2021 to 23.05.2021, on the selection screen. In the ALV output, the output must come as JAN 21, FEB21, MARCH21, APRIL21. along with the customer nos. Under Jan 21, the total count of the sales orders for Jan 21 must come. Also, if the user changes the date range, for example, 01.01.2021 to 01.04.2021, then the output must come as JAN21, FEB21, MARCH21 respectively. Please check the code and point out what changes, I need to do.

Regards,

Saurav Lahiry

1 REPLY 1
Read only

Sandra_Rossi
Active Contributor
0 Likes
481

Field catalog is obsolete. REUSE_ALV_GRID... is obsolete. Use CL_SALV_TABLE instead, no need of catalog.