Friday, August 7, 2015

Dynamic Internal table sap abap

*& DESCRIPTION :This program will display the meaning of a term in different
*& languages in which it is maintained in custtable  The program
*& will create Dynamic Internal table based on user Input on Selection screen.
*&---------------------------------------------------------------------*

REPORT yj_test_dynamic_int_tab1.


TYPES : BEGIN OF ty_static_st,
          termid TYPE yplm_termid,
          term TYPE yplm_term,
          spras TYPE spras,
          fterm TYPE yplm_fterm,
          sterm TYPE yplm_sterm,
          meaning TYPE yplm_vocab_meaning,
          fused TYPE yplm_term_fstused,
          status TYPE mstae.
TYPES : END OF ty_static_st.

TYPES : BEGIN OF ty_f4_st,
          termid TYPE yplm_termid,
          term TYPE yplm_term,
        END OF ty_f4_st.

DATA : gt_static TYPE TABLE OF ty_static_st,
       gt_static_tt TYPE TABLE OF ty_static_st,
       gt_f4_help TYPE TABLE OF ty_f4_st.

DATA  gv_termid TYPE yplm_termid.
DATA  gv_pos  TYPE i.

* Dynamic Table Declarations
DATA : gt_dyn_table  TYPE REF TO data,
       gs_line       TYPE REF TO data,
       gs_line1      TYPE REF TO data,
       gs_dyn_fcat   TYPE lvc_s_fcat,
       gt_dyn_fcat   TYPE lvc_t_fcat.

* Field Symbols Declarations
FIELD-SYMBOLS: <gs_line>, <gs_line1>,
               <gs_dyn_table> TYPE STANDARD TABLE,
               <gs>.

*----------------------------------------------------------------------------

SELECTION-SCREEN : BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
 SELECT-OPTIONS  s_termid FOR gv_termid.
SELECTION-SCREEN :END OF BLOCK b01.

*----------------------------------------------------------------------------

AT SELECTION-SCREEN.
  IF s_termid IS INITIAL.
    MESSAGE text-002 TYPE 'E'.
  ENDIF.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_termid-low.

  SELECT termid term FROM yplm_vocab_trans
    INTO TABLE gt_f4_help.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield    = 'TERMID'
      dynpprog    = 'YJ_TEST_DYNAMIC_INT_TAB1'
      dynpnr      = '1000'
      dynprofield = 'S_TERMID-LOW'
      value_org   = 'S'
    TABLES
      value_tab   = gt_f4_help.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_termid-high.

  REFRESH gt_f4_help.
  SELECT termid term FROM yplm_vocab_trans
    INTO TABLE gt_f4_help.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield    = 'TERMID'
      dynpprog    = 'YJ_TEST_DYNAMIC_INT_TAB1'
      dynpnr      = '1000'
      dynprofield = 'S_TERMID-HIGH'
      value_org   = 'S'
    TABLES
      value_tab   = gt_f4_help.


START-OF-SELECTION.

*Populating the static input table.
  SELECT termid term spras fterm sterm meaning fused status
       INTO TABLE gt_static
       FROM yplm_vocab_trans
       WHERE termid IN s_termid.


*Creating a dynamic structure and dynamic table
  PERFORM create_structure USING gt_static.


*Populating Dynamic Internal table
  PERFORM populate_dyn_table USING gt_static.


END-OF-SELECTION.
*Display Dynamic ALV O/P
  PERFORM display_alv USING gt_static.

*&---------------------------------------------------------------------*
*&      Form  create_structure
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->Pt_static  text
*----------------------------------------------------------------------*

FORM create_structure USING pt_static LIKE gt_static_tt.

  DATA: lv_fieldname(10),
        lv_coltext(10),
        lv_outputlen TYPE i,
        lv_key TYPE c.

  DATA  ls_static  TYPE ty_static_st.

  lv_fieldname = 'TERMID' . lv_outputlen = 12. lv_coltext = 'TERMID'. lv_key = abap_true.       " Creating termid column
  PERFORM create_column USING lv_fieldname
                              lv_outputlen
                              lv_coltext
                              lv_key.

  lv_fieldname = 'TERM' . lv_outputlen = 12. lv_coltext = 'TERM'.   lv_key = abap_true.        " Creating column term
  PERFORM create_column USING lv_fieldname
                              lv_outputlen
                              lv_coltext
                              lv_key.

* Loop through the internal table creating a column for every distinct language in the internal table
  LOOP AT pt_static INTO ls_static .

    DATA :lv_fieldname1(10) TYPE c,
          fname1  TYPE string,
          fname   TYPE string.

    READ TABLE gt_dyn_fcat INTO gs_dyn_fcat WITH KEY fieldname = ls_static-spras.
    IF sy-subrc NE 0.

      SELECT SINGLE sptxt FROM t002t INTO fname1 WHERE spras = 'E'
                                                  AND  sprsl = ls_static-spras.

      gv_pos = gv_pos + 1.
      CONCATENATE fname1 'FullDesc' INTO fname SEPARATED BY space.

      lv_fieldname = ls_static-spras . lv_outputlen = 10. lv_coltext = fname. lv_key = space.
      PERFORM create_column USING lv_fieldname
                                  lv_outputlen
                                  lv_coltext
                                  lv_key .

      CONCATENATE ls_static-spras 'S' INTO lv_fieldname1.

*    Adding Small desc Column for every language
      gv_pos = gv_pos + 1.
      CONCATENATE fname1 'SmallDesc' INTO fname SEPARATED BY space.

      lv_fieldname = lv_fieldname1 . lv_outputlen = 10. lv_coltext = fname. lv_key = space.
      PERFORM create_column USING lv_fieldname
                                  lv_outputlen
                                  lv_coltext
                                  lv_key.
    ENDIF.
    CLEAR gs_dyn_fcat.
  ENDLOOP.

  lv_fieldname = 'MEANING' . lv_outputlen = 14. lv_coltext = 'Term Meaning'. lv_key = space.        " Create column Meaning
  PERFORM create_column USING lv_fieldname
                              lv_outputlen
                              lv_coltext
                              lv_key.

  lv_fieldname = 'FUSED' . lv_outputlen = 14. lv_coltext = 'Term First Used'. lv_key = space.       " Create column Fused
  PERFORM create_column USING lv_fieldname
                              lv_outputlen
                              lv_coltext
                              lv_key.

  lv_fieldname = 'STATUS' . lv_outputlen = 14. lv_coltext = 'Status'. lv_key = space.                " Create column status
  PERFORM create_column USING lv_fieldname
                              lv_outputlen
                              lv_coltext
                              lv_key.

** Create a dynamic internal table with this structure.
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      i_style_table             = 'X'
      it_fieldcatalog           = gt_dyn_fcat
    IMPORTING
      ep_table                  = gt_dyn_table
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.

  IF sy-subrc EQ 0.

* Assign the new table to field symbol
    ASSIGN gt_dyn_table->* TO <gs_dyn_table>.

* Create dynamic work area for the dynamic table
    CREATE DATA gs_line  LIKE LINE OF <gs_dyn_table>.
    CREATE DATA gs_line1 LIKE LINE OF <gs_dyn_table>.
    ASSIGN gs_line->*  TO <gs_line>.
    ASSIGN gs_line1->* TO <gs_line1>.

  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  POPULATE_DYN_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->pt_static  text
*----------------------------------------------------------------------*
FORM populate_dyn_table  USING    pt_static LIKE gt_static_tt.

  DATA :ls_static  TYPE ty_static_st,
        ls_static1 TYPE ty_static_st,
        ls_line    TYPE REF TO data,
        ls_line1   TYPE REF TO data.

* Populate the dynamic table
  LOOP AT pt_static INTO ls_static.

* Avoid duplicate entries for key field TERM.
    READ TABLE <gs_dyn_table> INTO <gs_line1> WITH KEY ('TERM') = ls_static-term.
    IF sy-subrc = 0.
      CONTINUE.
    ENDIF.

* Assing values to the components other than Full Desc and Short Desc
    ASSIGN COMPONENT 'TERM' OF STRUCTURE <gs_line> TO <gs>.
    <gs> = ls_static-term.
    ASSIGN COMPONENT 'TERMID' OF STRUCTURE <gs_line> TO <gs>.
    <gs> = ls_static-termid.
    ASSIGN COMPONENT 'MEANING' OF STRUCTURE <gs_line> TO <gs>.
    <gs> = ls_static-meaning.
    ASSIGN COMPONENT 'FUSED' OF STRUCTURE <gs_line> TO <gs>.
    <gs> = ls_static-fused.
    ASSIGN COMPONENT 'STATUS' OF STRUCTURE <gs_line> TO <gs>.
    <gs> = ls_static-status.

    UNASSIGN <gs>.

* Loop on fcat to assign values to fterm and sterm
    LOOP AT gt_dyn_fcat INTO gs_dyn_fcat.

      DATA lv_fieldname1(10) TYPE c.
      IF gs_dyn_fcat-fieldname = 'TERM'.
        CONTINUE.
      ENDIF.

      READ TABLE pt_static INTO ls_static1 WITH KEY term = ls_static-term
                                                        spras = gs_dyn_fcat-fieldname .
      IF sy-subrc = 0.
        ASSIGN COMPONENT gs_dyn_fcat-fieldname OF STRUCTURE <gs_line> TO <gs>.
        <gs> = ls_static1-fterm.
*      UNASSIGN <gs>.

        CONCATENATE gs_dyn_fcat-fieldname 'S' INTO lv_fieldname1.
        ASSIGN COMPONENT lv_fieldname1 OF STRUCTURE <gs_line> TO <gs>.
        <gs> = ls_static1-sterm.
      ENDIF.

      UNASSIGN <gs>.
      CLEAR : ls_static1.

    ENDLOOP.

* Appending the value to the dynamic Internal table
    APPEND <gs_line> TO <gs_dyn_table>.
    CLEAR: <gs_line>.
    CLEAR: ls_static, ls_static1.

  ENDLOOP.
ENDFORM.                    " POPULATE_DYN_TABLE
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->pt_static  text
*----------------------------------------------------------------------*
FORM display_alv  USING pt_static.

  DATA : ls_alv_fieldcat  TYPE slis_fieldcat_alv,
         lt_alv_fieldcat  TYPE slis_t_fieldcat_alv.
  DATA lv_pos TYPE i.

  LOOP AT gt_dyn_fcat INTO gs_dyn_fcat.
    lv_pos = lv_pos + 1.
    ls_alv_fieldcat-fieldname     = gs_dyn_fcat-fieldname.
    ls_alv_fieldcat-tabname       = gs_dyn_fcat-tabname.
    ls_alv_fieldcat-seltext_l     = gs_dyn_fcat-coltext.
    ls_alv_fieldcat-outputlen     = gs_dyn_fcat-outputlen.
    ls_alv_fieldcat-col_pos       = lv_pos.
    ls_alv_fieldcat-do_sum        = gs_dyn_fcat-do_sum.
    ls_alv_fieldcat-emphasize     = gs_dyn_fcat-emphasize.
    ls_alv_fieldcat-key           = gs_dyn_fcat-key.
    ls_alv_fieldcat-no_out        = gs_dyn_fcat-no_out.
    APPEND ls_alv_fieldcat TO lt_alv_fieldcat.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = lt_alv_fieldcat
      i_default          = 'X'
      i_save             = 'A'
    TABLES
      t_outtab           = <gs_dyn_table>.
ENDFORM.                    " DISPLAY_ALV
*&---------------------------------------------------------------------*
*&      Form  CREATE_COLUMN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PV_FIELDNAME  text
*      -->PV_OUTPUTLEN  text
*      -->PV_COLTEXT  text
*----------------------------------------------------------------------*
FORM create_column  USING    pv_fieldname TYPE c
                             pv_outputlen TYPE i
                             pv_coltext   TYPE c
                             pv_key       TYPE c.

** This would create structure Text Lang Full-desc Short-desc  etc ....
  gv_pos = gv_pos + 1.
  gs_dyn_fcat-fieldname = pv_fieldname.          "Term id column
  gs_dyn_fcat-outputlen = pv_outputlen.
  gs_dyn_fcat-tabname   = 'IT_DYNAMIC'.
  gs_dyn_fcat-coltext   = pv_coltext.
  gs_dyn_fcat-col_pos   = gv_pos.
  gs_dyn_fcat-key       = pv_key.
  gs_dyn_fcat-key_sel   = pv_key.
  APPEND gs_dyn_fcat TO gt_dyn_fcat.
  CLEAR gs_dyn_fcat.

ENDFORM.                    " CREATE_COLUMN

Dynamic Internal table SAP ABAP

*&---------------------------------------------------------------------*
*& Report  YJ_TEST_DYNAIC_INT_TAB
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT YJ_TEST_DYNAIC_INT_TAB.
TYPES : BEGIN OF gfirst_typ,
 vend(6) TYPE c,
 month(5) TYPE c,
 amt TYPE i.
 TYPES : END OF gfirst_typ.

DATA : it_zdemo TYPE TABLE OF gfirst_typ.
 DATA : wa_zdemo LIKE LINE OF it_zdemo,
 wa_zdemo1 LIKE LINE OF it_zdemo.

DATA : gv_pos TYPE i.
 DATA : fname TYPE string.

* Dynamic Table Declarations


 DATA : gt_dyn_table  TYPE REF TO data,
 gw_line       TYPE REF TO data,
 gw_line1       TYPE REF TO data,
 gw_dyn_fcat         TYPE lvc_s_fcat,
 gt_dyn_fcat         TYPE lvc_t_fcat.

* Field Symbols Declarations



FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
 <gfs_dyn_table> TYPE STANDARD TABLE,
 <fs1>.

* Populate the initial input table. Usually this input table contents will be populated at run time, which raises the requirement of dynamic table. The table contents are filled here for illustration purpose.


wa_zdemo-vend = 'V100'.
 wa_zdemo-month = 'JAN'.
 wa_zdemo-amt = 100.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.
 wa_zdemo-month = 'FEB'.
 wa_zdemo-amt = 200.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
 wa_zdemo-month = 'FEB'.
 wa_zdemo-amt = 200.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.
 wa_zdemo-month = 'FEB'.
 wa_zdemo-amt = 150.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
 wa_zdemo-month = 'MAR'.
 wa_zdemo-amt = 250.
 APPEND wa_zdemo TO it_zdemo.


wa_zdemo-vend = 'V300'.
 wa_zdemo-month = 'MAR'.
 wa_zdemo-amt = 300.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V100'.
 wa_zdemo-month = 'APR'.
 wa_zdemo-amt = 200.
 APPEND wa_zdemo TO it_zdemo.



wa_zdemo-vend = 'V100'.
 wa_zdemo-month = 'MAY'.
 wa_zdemo-amt = 100.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V200'.
 wa_zdemo-month = 'MAY'.
 wa_zdemo-amt = 50.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V300'.
 wa_zdemo-month = 'MAY'.
 wa_zdemo-amt = 125.
 APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = 'V400'.
 wa_zdemo-month = 'MAY'.
 wa_zdemo-amt = 475.
 APPEND wa_zdemo TO it_zdemo.



Write : / 'Initial Internal Table'.

WRITE :/.
 write :/(6) 'Vendor'.
 write : (12) 'Month' .
 write : (3) 'Amt' .
 LOOP AT it_zdemo INTO wa_zdemo.
 WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.
 ENDLOOP.



** This would create structure Vendor Jan13 Feb13 Mar13 etc ....


 gv_pos = gv_pos + 1.
 gw_dyn_fcat-fieldname = 'VEND'.
 gw_dyn_fcat-outputlen = 5.
 gw_dyn_fcat-tabname   = 'IT_DEMO'.
 gw_dyn_fcat-coltext   = 'VENDOR'.
 gw_dyn_fcat-col_pos   = gv_pos.
 gw_dyn_fcat-key = 'X'.
 gw_dyn_fcat-key_sel = 'X'.
 APPEND gw_dyn_fcat TO gt_dyn_fcat.

clear gw_dyn_fcat.



* Loop through the internal table creating a column for every distinct month in the internal table

LOOP AT it_zdemo INTO wa_zdemo.
 gv_pos = gv_pos + 1.
 CONCATENATE wa_zdemo-month '13' INTO fname.
 read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.
 if sy-subrc NE 0.
 gw_dyn_fcat-fieldname = wa_zdemo-month.
 gw_dyn_fcat-tabname   = 'IT_DEMO'.
 gw_dyn_fcat-coltext   = fname.
 gw_dyn_fcat-outputlen = 10.
 gw_dyn_fcat-col_pos   = gv_pos.
 APPEND gw_dyn_fcat TO gt_dyn_fcat.
 endif.
 clear gw_dyn_fcat.
 ENDLOOP.




** Create a dynamic internal table with this structure.

CALL METHOD cl_alv_table_create=>create_dynamic_table
 EXPORTING
 i_style_table             = 'X'
 it_fieldcatalog           = gt_dyn_fcat
 IMPORTING
 ep_table                  = gt_dyn_table
 EXCEPTIONS
 generate_subpool_dir_full = 1
 OTHERS                    = 2.

IF sy-subrc EQ 0.
* Assign the new table to field symbol
 ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
 CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
 CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
 ASSIGN gw_line->* TO <gfs_line>.
 ASSIGN gw_line1->* TO <gfs_line1>.
 ENDIF.




* Populate the dynamic table

LOOP AT it_zdemo INTO wa_zdemo.


* Avoid duplicate entries for key field PART.
 READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('VEND') = wa_zdemo-vend.
 IF sy-subrc = 0.
 CONTINUE.
 ENDIF.


 ASSIGN COMPONENT 'VEND' OF STRUCTURE <gfs_line> TO <fs1>.
 <fs1> = wa_zdemo-vend.
 UNASSIGN <fs1>.


 LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
 IF gw_dyn_fcat-fieldname = 'VEND'.
 CONTINUE.
 ENDIF.
 READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTO wa_zdemo1.
 IF sy-subrc = 0.
 ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
 <fs1> = wa_zdemo1-amt.
 UNASSIGN <fs1>.
 ENDIF.
 clear : wa_zdemo1.
 ENDLOOP.
 APPEND <gfs_line> TO <gfs_dyn_table>.
 CLEAR: <gfs_line>.
 clear: wa_zdemo, wa_zdemo1.
 ENDLOOP.


WRITE :/.

Write : / 'Dynamic Internal Table'.
 WRITE :/.
 LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
 WRITE (10) : gw_dyn_fcat-coltext.
 ENDLOOP.
 WRITE :/.
 LOOP AT <gfs_dyn_table> INTO <gfs_line>.
 LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
 ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
 WRITE : <fs1>.
 ENDLOOP.
 WRITE :/      .
 ENDLOOP.

Userexit determining program sap abap

*&---------------------------------------------------------------------*
*& Report  YJUSEREXIT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT YJUSEREXIT.
*report z_find_user_exit no standard page heading.



tables: tstc, tadir, modsapt, modact,

        trdir, tfdir, enlfdir, tstct.



data : jtab like tadir occurs 0 with header line.

data : hotspot(30).



parameters : p_tcode like tstc-tcode obligatory.



at line-selection.

  get cursor field hotspot.

  check hotspot(4) eq 'JTAB'.

  set parameter id 'MON' field sy-lisel+1(10).

  call transaction 'SMOD' and skip first screen.



start-of-selection.



  perform get_data.

  perform write_list.





*----

*--------------------------------------------------------------------------------


*      FORM get_data                                                 *


*----

*--------------------------------------------------------------------------------


form get_data.



  select single * from tstc

              where tcode eq p_tcode.

  check sy-subrc eq 0.



  select single * from tadir

            where pgmid = 'R3TR'

              and object = 'PROG'

              and obj_name = tstc-pgmna.





  if sy-subrc ne 0.



    select single * from trdir

             where name = tstc-pgmna.

    if trdir-subc eq 'F'.

      select single * from tfdir

                     where pname = tstc-pgmna.

      select single * from enlfdir

                     where funcname = tfdir-funcname.

      select single * from tadir

                     where pgmid = 'R3TR'

                       and object = 'FUGR'

                       and obj_name eq enlfdir-area.

    endif.



  endif.



  select * from tadir into table jtab

             where pgmid = 'R3TR'

               and object = 'SMOD'

               and devclass = tadir-devclass.



  select single * from tstct

          where sprsl eq sy-langu

            and tcode eq p_tcode.



endform.



*----

*--------------------------------------------------------------------------------


*      FORM write_list                                               *


*----

*--------------------------------------------------------------------------------


form write_list.



  format color col_positive intensified off.

  write:/(19) 'Transaction Code - ',

  20(20) p_tcode,

  45(50) tstct-ttext.

  skip.

  if not jtab[] is initial.

    write:/(95) sy-uline.

    format color col_heading intensified on.

    write:/1 sy-vline,

    2 'Exit Name',

    21 sy-vline ,

    22 'Description',

    95 sy-vline.

    write:/(95) sy-uline.

    loop at jtab.

      select single * from modsapt

      where sprsl = sy-langu and

      name = jtab-obj_name.

      format color col_normal intensified off.

      write:/1 sy-vline,

      2 jtab-obj_name hotspot on,

      21 sy-vline ,

      22 modsapt-modtext,

      95 sy-vline.

    endloop.

    write:/(95) sy-uline.

    describe table jtab.

    skip.

    format color col_total intensified on.

    write:/ 'No of Exits:' , sy-tfill.

  else.

    format color col_negative intensified on.

    write:/(95) 'No User Exit exists'.

  endif.



endform.