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

Very complex scenario in field symbols.

former_member574106
Participant
0 Kudos
1,706
Hi friends,
I am solving my first field symbol scenario, which is extremely complex.
 The scenario,  is that there is a selection screen with two fields date and sales organization 
as select options.  The alv output needs to be generated dynamically. 
This means that if the user enters the range as 01.03.2021 to 01.05.2021, 
then in the ALV grid, only March and April must be displayed. 
Also, in March column, the total no of sales order nos( VBELN ) must be given. 
Similar, is the case in April. For example, if i enter 01.03.2021 to say 31.05.2021
, then, March, April & May must be displayed,  along with the total no of sales 
orders  for that month respectively. Please check my code and tell, how to proceed.  
Thanks a lot for your help.

*&---------------------------------------------------------------------*
*& Include          ZDYAMICOOPS2_TOP
*&---------------------------------------------------------------------*


TABLES : vbak.
TYPE-POOLS : slis.
SELECT-OPTIONS : so_erdat FOR vbak-erdat,
                 so_vkorg FOR vbak-vkorg.


TYPES : BEGIN OF ty_vbak,
        vbeln TYPE vbak-vbeln,
        erdat TYPE vbak-erdat,
        ernam TYPE vbak-ernam,
        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.


* 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>.
.


*&---------------------------------------------------------------------*
*& Report ZDYNAMICOOPS2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdynamicoops2.
INCLUDE zdyamicoops2_top.
PERFORM fetch_data.
PERFORM build_table.
PERFORM build_fieldcatalog.
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM fetch_data .


SELECT vbeln
       erdat
       ernam
       FROM vbak INTO TABLE it_vbak
       WHERE vkorg IN so_vkorg
       AND   erdat IN so_erdat.
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.


ENDLOOP.




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






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


DO l_month TIMES.
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  wa_fldcat.
ENDDO.


* Craeate dynamic table and assign it to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING

    it_fieldcatalog           = gt_dyn_fcat
*    i_length_in_byte          =
  IMPORTING
    ep_table                  = gt_dyn_table

  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>.


ENDFORM.
7 REPLIES 7
Read only

Sandra_Rossi
Active Contributor
1,610

Don't use cl_alv_table_create=>create_dynamic_table, use RTTC instead. See there if you don't want to spend time on learning RTTC now.

I'd be happy to answer a precise question on a small topic, but not to tell you how I would create a program to fulfill a whole business requirement.

Read only

matt
Active Contributor
1,610

FORMs, TABLES statements. Mixed naming "standards" (what's an internal table, it_ or gt_?) I think you should take a long hard look at the SAP programming style guide. Or clean code. Stop using obsolete elements of ABAP.

Using cl_alv_table_create=>create_dynamic_table? That's wrong. You should use RTTS

You've a completely untyped field-symbol. At least use TYPE ANY.

You need to think again about using a select option for a date range. What if the use enters E EQ 01.01.2004? https://blogs.sap.com/2014/02/07/dates-and-select-options/

How big can the range be? If it's maximum 12 months, I can think of a solution that involves no field-symbols at all and no dynamic programming.

Read only

former_member574106
Participant
0 Kudos
1,610

Hi Mathew,

As per your suggestions, i made changes in the code. i kept I_ for internal tables and wa_ for work areas, except field symbols. While using ZCL_ALV_TABLE_CREATE_RTTC, this error is coming. Another error is that, while using do and enddo loop, the debugger is not coming to the executable statement, inside the loop. <a href="/storage/temp/1907900-debugging-2.jpg">debugging-2.jpg</a> please check the code. 



*&---------------------------------------------------------------------*
*& Include          ZDYAMICOOPS2_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,
        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.


* Fieldcatalog declarations


DATA : it_dyn_fcat TYPE lvc_t_fcat,
       wa_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 ANY,
                <dyn_wa>.
.


*&---------------------------------------------------------------------*
*& Report ZDYNAMICOOPS2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdynamicoops2.
INCLUDE zdyamicoops2_top.
PERFORM fetch_data.
PERFORM build_table.
PERFORM build_fieldcatalog.
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM fetch_data .


SELECT vbeln
       erdat
       ernam
       FROM vbak INTO TABLE it_vbak
       WHERE vkorg IN so_vkorg
       AND   erdat IN so_erdat.
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.


ENDLOOP.




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






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


DO l_month TIMES.


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  wa_fldcat.
*ENDIF.
ENDDO.


* Craeate dynamic table and assign it to FS


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>.


ENDFORM.


Read only

0 Kudos
1,610

The "Your Answer" field on the forum is the answer to the question. Use a comment instead.

Also, you've not answered the questions I asked in my comment.

Read only

matt
Active Contributor
0 Kudos
1,610

Why do you think that using zcl_alv_table_create=>create_dynamic_table instead of cl_alv_table_create=>create_dynamic_table will make a difference?

You have not answered the questions in my previous comment.

Read only

Sandra_Rossi
Active Contributor
1,610

Code of zcl_alv_table_create must be downloaded as explained there, and activated of course so that you can use it.

Read only

former_member574106
Participant
0 Kudos
1,610

Hi Mathew,

Thanks a lot for the tips. Made the necessary changes. But, a new error is coming, while executing. Please check the screenshots and the code.

Regards,

*&---------------------------------------------------------------------*
*& 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,
        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.
.


*&---------------------------------------------------------------------*
*& Report ZDYNAMICOOPS2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdynamicoops2.
INCLUDE ZDYNAMICOOPS2_top.
PERFORM fetch_data.
PERFORM build_table.
PERFORM build_fieldcatalog.
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM fetch_data .


SELECT vbeln
       erdat
       ernam
       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 = 'ERDAT'.
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.


ENDDO.


* Craeate dynamic table and assign it to FS


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 'ERNAM' OF STRUCTURE <dyn_wa> TO <gv_field>.
IF sy-subrc = 0.
<gv_field> = wa_vbak-ernam.
ENDIF.


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


APPEND <dyn_wa> TO <dyn_table>.
ENDLOOP.
ENDFORM.
debugging.jpg