Nov 18, 2010

Create internal table structure dynamically

*&-------------------------------------------------------------*
*& Report  CREATE_DYNAMIC_ITAB                                 *
*&                                                             *
*&-------------------------------------------------------------*
*& Example of creating an ABAP itab dynamically                *
*& Then populate dynamic table and display using ALV           *
*&-------------------------------------------------------------*
*&-Created By details------------------------------------------*
*&                                                             *
*& Author : www.sapdev.co.uk                                   *
*&  SAP ABAP development *
*&-------------------------------------------------------------*
REPORT  CREATE_DYNAMIC_ITAB.

type-pools : abap, slis.
field-symbols: <fs_table> type standard table,
               <fs_wa>,
               <fs_field>.
data: dyn_table    type ref to data,
      dyn_line     type ref to data,
      wa_fieldcat type lvc_s_fcat,
      it_fieldcat type lvc_t_fcat.

*ALV data declarations
data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_tab_group type slis_t_sp_group_alv,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     like sy-repid.

selection-screen begin of block block1 with frame.
parameters: p_table(30) type c default 'SFLIGHT'.
selection-screen end of block block1.

***********************************************************************
*start-of-selection.
start-of-selection.
  perform get_table_structure.
  perform create_itab_dynamically.
  perform get_data.
  perform display_data.

*&---------------------------------------------------------------------*
*&      Form  get_table_structure
*&---------------------------------------------------------------------*
*       Get structure of an SAP table
*----------------------------------------------------------------------*
form get_table_structure.
  data : it_tabdescr type abap_compdescr_tab,
         wa_tabdescr type abap_compdescr.
  data : ref_table_descr type ref to cl_abap_structdescr.

* Return structure of the table.
  ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
  it_tabdescr[] = ref_table_descr->components[].
  loop at it_tabdescr into wa_tabdescr.
    clear wa_fieldcat.
    wa_fieldcat-fieldname = wa_tabdescr-name .
    wa_fieldcat-datatype  = wa_tabdescr-type_kind.
    wa_fieldcat-inttype   = wa_tabdescr-type_kind.
    wa_fieldcat-intlen    = wa_tabdescr-length.
    wa_fieldcat-decimals  = wa_tabdescr-decimals.
    append wa_fieldcat to it_fieldcat.
  endloop.
endform.                    "get_table_structure

*&---------------------------------------------------------------------*
*&      Form  create_itab_dynamically
*&---------------------------------------------------------------------*
*       Create internal table dynamically
*----------------------------------------------------------------------*
form create_itab_dynamically.
* Create dynamic internal table and assign to Field-Symbol
  call method cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_fieldcat
    IMPORTING
      ep_table        = dyn_table.
  assign dyn_table->* to <fs_table>.
* Create dynamic work area and assign to Field Symbol
  create data dyn_line like line of <fs_table>.
  assign dyn_line->* to <fs_wa>.
endform.                    "create_itab_dynamically

*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       Populate dynamic itab
*----------------------------------------------------------------------*
form get_data.
* Select Data from table using field symbol which points to dynamic itab
  select * into CORRESPONDING FIELDS OF TABLE  <fs_table>
             from (p_table).
endform.                    "get_data

*&---------------------------------------------------------------------*
*&      Form  display_data
*&---------------------------------------------------------------------*
*       display data using ALV
*----------------------------------------------------------------------*
FORM display_data.
  perform build_fieldcatalog.
  perform build_layout.
  perform display_alv_report.
ENDFORM.                    " display_data

*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report, using SAP table structure
*----------------------------------------------------------------------*
form build_fieldcatalog.
* ALV Function module to build field catalog from SAP table structure
  DATA: it_fcat  TYPE slis_t_fieldcat_alv.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = p_table
    CHANGING
      ct_fieldcat            = it_fcat
    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.

  fieldcatalog[] =  it_fcat[].
endform.                    " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
  gd_layout-colwidth_optimize = 'X'.
endform.                    " BUILD_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
  gd_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = gd_repid
      is_layout          = gd_layout
      it_fieldcat        = fieldcatalog[]
      i_save             = 'X'
    TABLES
      t_outtab           = <fs_table>
    EXCEPTIONS
      program_error      = 1
      others             = 2.
endform.                    " DISPLAY_ALV_REPORT



Nov 13, 2010

Multi-colored output of ALV in EXCEL

There would be many situations where we need to highlight some of the records in bold or different color depending on some value. In this document, we showcase how this can be achieved. Look at the screenshot of an excel sheet with different colors and bold in some cells.






Following is a demo program in achieving the same:

REPORT zmulticolor_test NO STANDARD PAGE HEADING.


* this report demonstrates how to send some ABAP data to an
* EXCEL sheet using OLE automation.
INCLUDE ole2incl.


* handles for OLE objects
DATA: h_excel TYPE ole2_object,        " Excel object
      h_mapl TYPE ole2_object,         " list of workbooks
      h_map TYPE ole2_object,          " workbook
      h_zl TYPE ole2_object,           " cell
      h_f TYPE ole2_object,            " font
      h_c TYPE ole2_object.            " color

DATA: filename LIKE rlgrap-filename.

TABLES: spfli.
DATA  h TYPE i.


* table of flights
DATA: it_spfli LIKE spfli OCCURS 10 WITH HEADER LINE.


*&---------------------------------------------------------------------*

*&   Event START-OF-SELECTION
*&---------------------------------------------------------------------*

START-OF-SELECTION.
* read flights
  SELECT * FROM spfli INTO TABLE it_spfli.
* display header
  ULINE (61).
  WRITE: /     sy-vline NO-GAP,
          (3)  'Flg'(001COLOR COL_HEADING NO-GAP, sy-vline NO-GAP,
          (4)  'Nr'(002COLOR COL_HEADING NO-GAP, sy-vline NO-GAP,
          (20'Von'(003COLOR COL_HEADING NO-GAP, sy-vline NO-GAP,
          (20'Nach'(004COLOR COL_HEADING NO-GAP, sy-vline NO-GAP,
          (8)  'Zeit'(005COLOR COL_HEADING NO-GAP, sy-vline NO-GAP.
  ULINE /(61).
* display flights
  LOOP AT it_spfli.
    WRITE: / sy-vline NO-GAP,
             it_spfli-carrid COLOR COL_KEY NO-GAP, sy-vline NO-GAP,
             it_spfli-connid COLOR COL_NORMAL NO-GAP, sy-vline NO-GAP,
             it_spfli-cityfrom COLOR COL_NORMAL NO-GAP, sy-vline NO-GAP,
             it_spfli-cityto COLOR COL_NORMAL NO-GAP, sy-vline NO-GAP,
             it_spfli-deptime COLOR COL_NORMAL NO-GAP, sy-vline NO-GAP.
  ENDLOOP.
  ULINE /(61).
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           text       = text-007
       EXCEPTIONS
            OTHERS     = 1.
* start Excel
  CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
*  PERFORM ERR_HDL.

  SET PROPERTY OF h_excel  'Visible' = 1.
*  CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls'  .

*  PERFORM ERR_HDL.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           text       = text-008
       EXCEPTIONS
            OTHERS     = 1.
* get list of workbooks, initially empty
  CALL METHOD OF h_excel 'Workbooks' = h_mapl.
  PERFORM err_hdl.
* add a new workbook
  CALL METHOD OF h_mapl 'Add' = h_map.
  PERFORM err_hdl.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           text       = text-009
       EXCEPTIONS
            OTHERS     = 1.
* output column headings to active Excel sheet
  PERFORM fill_cell USING 1 1 1 200 'Carrier id'(001).
  PERFORM fill_cell USING 1 2 1 200 'Connection id'(002).
  PERFORM fill_cell USING 1 3 1 200 'City from'(003).
  PERFORM fill_cell USING 1 4 1 200 'City to'(004).
  PERFORM fill_cell USING 1 5 1 200 'Dep. Time'(005).
  LOOP AT it_spfli.
* copy flights to active EXCEL sheet
    h = sy-tabix + 1.
    IF it_spfli-carrid CS 'AA'.
      PERFORM fill_cell USING h 1 0 000255000 it_spfli-carrid.
    ELSEIF it_spfli-carrid CS 'AZ'.
      PERFORM fill_cell USING h 1 0 168000000 it_spfli-carrid.
    ELSEIF it_spfli-carrid CS 'JL'.
      PERFORM fill_cell USING h 1 0 168168000 it_spfli-carrid.
    ELSEIF it_spfli-carrid CS 'LH'.
      PERFORM fill_cell USING h 1 0 111111111 it_spfli-carrid.
    ELSEIF it_spfli-carrid CS 'SQ'.
      PERFORM fill_cell USING h 1 0 100100100 it_spfli-carrid.
    ELSE.
      PERFORM fill_cell USING h 1 0 000145000 it_spfli-carrid.
    ENDIF.

    IF it_spfli-connid LT 400.
      PERFORM fill_cell USING h 2 0 255000255 it_spfli-connid.
    ELSEIF it_spfli-connid LT 800.
      PERFORM fill_cell USING h 2 0 077099088 it_spfli-connid.
    ELSE.
      PERFORM fill_cell USING h 2 0 246156138 it_spfli-connid.
    ENDIF.

    IF it_spfli-cityfrom CP 'S*'.
      PERFORM fill_cell USING h 3 0 155155155 it_spfli-cityfrom.
    ELSEIF it_spfli-cityfrom CP 'N*'.
      PERFORM fill_cell USING h 3 0 189111222 it_spfli-cityfrom.
    ELSE.
      PERFORM fill_cell USING h 3 0 111230222 it_spfli-cityfrom.
    ENDIF.

    IF it_spfli-cityto CP 'S*'.
      PERFORM fill_cell USING h 4 0 200200200 it_spfli-cityto.
    ELSEIF it_spfli-cityto CP 'N*'.
      PERFORM fill_cell USING h 4 0 000111222 it_spfli-cityto.
    ELSE.
      PERFORM fill_cell USING h 4 0 130230230 it_spfli-cityto.
    ENDIF.

    IF it_spfli-deptime LT '020000'.
      PERFORM fill_cell USING h 5 0 145145145 it_spfli-deptime.
    ELSEIF it_spfli-deptime LT '120000' .
      PERFORM fill_cell USING h 5 0 015215205 it_spfli-deptime.
    ELSEIF it_spfli-deptime LT '180000' .
      PERFORM fill_cell USING h 5 0 000215205 it_spfli-deptime.
    ELSE.
      PERFORM fill_cell USING h 5 0 115115105 it_spfli-deptime.
    ENDIF.
  ENDLOOP.

* EXCEL FILENAME
  CONCATENATE sy-repid '_' sy-datum+6(2'_' sy-datum+4(2'_'
              sy-datum(4'_' sy-uzeit '.XLS' INTO filename.

  CALL METHOD OF h_map 'SAVEAS' EXPORTING #1 = filename.

  FREE OBJECT h_excel.
  PERFORM err_hdl.


*---------------------------------------------------------------------*
*       FORM FILL_CELL                                                *
*---------------------------------------------------------------------*
*       sets cell at coordinates i,j to value val boldtype bold       *
*---------------------------------------------------------------------*
FORM fill_cell USING i j bold col val.
  CALL METHOD OF h_excel 'Cells' = h_zl
    EXPORTING
      #1 = i
      #2 = j.
  PERFORM err_hdl.
  SET PROPERTY OF h_zl 'Value' = val .
  PERFORM err_hdl.
  GET PROPERTY OF h_zl 'Font' = h_f.
  PERFORM err_hdl.
  SET PROPERTY OF h_f 'Bold' = bold .
  PERFORM err_hdl.
  SET PROPERTY OF h_f 'Color' = col.
  PERFORM err_hdl.
ENDFORM.                    "FILL_CELL


*&---------------------------------------------------------------------*
*&      Form  ERR_HDL
*&---------------------------------------------------------------------*
*       outputs OLE error if any                                       *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

FORM err_hdl.
  IF sy-subrc <> 0.
    WRITE: / 'OLE-Automation Error:'(010), sy-subrc.
    STOP.
  ENDIF.
ENDFORM.                    " ERR_HDL



ref: http://www.saptechnical.com/Tutorials/ALV/ColoredOutput/Program.htm

Display subtotal text in ALV grid

Code:
*&---------------------------------------------------------------------*
*& Report  Z_ALV_SUBTOTAL
*&
*&---------------------------------------------------------------------
REPORT z_alv_subtotal.
*&---------------------------------------------------------------------*
*& Table declaration
*&---------------------------------------------------------------------*
TABLES: ekko.
*&---------------------------------------------------------------------*
*& Type pool declaration
*&---------------------------------------------------------------------*
TYPE-POOLS: slis. " Type pool for ALV
*&---------------------------------------------------------------------*
*& Selection screen
*&---------------------------------------------------------------------*
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.
*&---------------------------------------------------------------------*
*& Type declaration
*&---------------------------------------------------------------------*
* Type declaration for internal table to store EKPO data
TYPES: BEGIN OF x_data,
       ebeln  TYPE char30,  " Document no.
       ebelp  TYPE ebelp,   " Item no
       matnr  TYPE matnr,   " Material no
       matnr1 TYPE matnr,   " Material no
       werks  TYPE werks_d, " Plant
       werks1 TYPE werks_d, " Plant
       ntgew  TYPE entge,   " Net weight
       gewe   TYPE egewe,   " Unit of weight                      
       END OF x_data.
*&---------------------------------------------------------------------*
*& Internal table declaration
*&---------------------------------------------------------------------*
DATA:
* Internal table to store EKPO data
  i_ekpo TYPE STANDARD TABLE OF x_data INITIAL SIZE 0,
* Internal table for storing field catalog information
  i_fieldcat TYPE slis_t_fieldcat_alv,
* Internal table for Top of Page info. in ALV Display
  i_alv_top_of_page TYPE slis_t_listheader,
* Internal table for ALV Display events
  i_events TYPE slis_t_event,
* Internal table for storing ALV sort information
  i_sort TYPE  slis_t_sortinfo_alv,
  i_event TYPE slis_t_event.
*&---------------------------------------------------------------------*
*& Work area declaration
*&---------------------------------------------------------------------*
DATA:
  wa_ekko TYPE x_data,
  wa_layout     TYPE slis_layout_alv,
  wa_events         TYPE slis_alv_event,
  wa_sort TYPE slis_sortinfo_alv.
*&---------------------------------------------------------------------*
*& Constant declaration
*&---------------------------------------------------------------------*
CONSTANTS:
   c_header   TYPE char1
              VALUE 'H',                    "Header in ALV
   c_item     TYPE char1
              VALUE 'S'.
*&---------------------------------------------------------------------*
*& Start-of-selection event
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* Select data from ekpo
  SELECT ebeln " Doc no
         ebelp " Item
         matnr " Material
         matnr " Material
         werks " Plant
         werks " Plant
         ntgew " Quantity
         gewei " Unit
         FROM ekpo
         INTO TABLE i_ekpo
         WHERE ebeln IN s_ebeln
         AND ntgew NE '0.00'.
  IF sy-subrc = 0.
    SORT i_ekpo BY ebeln ebelp matnr .
  ENDIF.
* To build the Page header
  PERFORM sub_build_header.
* To prepare field catalog
  PERFORM sub_field_catalog.
* Perform to populate the layout structure
  PERFORM sub_populate_layout.
* Perform to populate the sort table.
  PERFORM sub_populate_sort.
* Perform to populate ALV event
  PERFORM sub_get_event.
END-OF-SELECTION.
* Perform to display ALV report
  PERFORM sub_alv_report_display.
*&---------------------------------------------------------------------*
*&      Form  sub_build_header
*&---------------------------------------------------------------------*
*       To build the header
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_build_header .
* Local data declaration
  DATA: l_system     TYPE char10 ,          "System id
        l_r_line     TYPE slis_listheader,  "Hold list header
        l_date       TYPE char10,           "Date
        l_time       TYPE char10,           "Time
        l_success_records TYPE i,           "No of success records
        l_title(300) TYPE c.                " Title
* Title  Display
  l_r_line-typ = c_header.               " header
  l_title = 'Test report'(001).
  l_r_line-info = l_title.
  APPEND l_r_line TO i_alv_top_of_page.
  CLEAR l_r_line.
* Run date Display
  CLEAR l_date.
  l_r_line-typ  = c_item.                " Item
  WRITE: sy-datum  TO l_date MM/DD/YYYY.
  l_r_line-key = 'Run Date :'(002).
  l_r_line-info = l_date.
  APPEND l_r_line TO i_alv_top_of_page.
  CLEAR: l_r_line,
         l_date.
ENDFORM.                    " sub_build_header
*&---------------------------------------------------------------------*
*&      Form  sub_field_catalog
*&---------------------------------------------------------------------*
*       Build Field Catalog
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_field_catalog .
*  Build Field Catalog
  PERFORM sub_fill_alv_field_catalog USING:
     '01' '01' 'EBELN' 'I_EKPO' 'L'
     'Doc No'(003) ' ' ' ' ' ' ' ',
     '01' '02' 'EBELP' 'I_EKPO' 'L'
     'Item No'(004) 'X' 'X' ' ' ' ',
     '01' '03' 'MATNR' 'I_EKPO' 'L'
     'Material No'(005) 'X' 'X' ' ' ' ',
     '01' '03' 'MATNR1' 'I_EKPO' 'L'
     'Material No'(005) ' ' ' ' ' ' ' ',
     '01' '04' 'WERKS' 'I_EKPO' 'L'
     'Plant'(006) 'X' 'X' ' ' ' ',
     '01' '04' 'WERKS1' 'I_EKPO' 'L'
     'Plant'(006) ' ' ' ' ' ' ' ',
     '01' '05' 'NTGEW' 'I_EKPO' 'R'
     'Net Weight'(007) ' ' ' ' 'GEWE' 'I_EKPO'.
ENDFORM.                    " sub_field_catalog
*&---------------------------------------------------------------------*
*&     Form  sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
*&     For building Field Catalog
*&---------------------------------------------------------------------*
*&     p_rowpos   Row position
*&     p_colpos   Col position
*&     p_fldnam   Fldname
*&     p_tabnam   Tabname
*&     p_justif   Justification
*&     p_seltext  Seltext
*&     p_out      no out
*&     p_tech     Technical field
*&     p_qfield   Quantity field
*&     p_qtab     Quantity table
*&---------------------------------------------------------------------*
FORM sub_fill_alv_field_catalog  USING  p_rowpos    TYPE sycurow
                                        p_colpos    TYPE sycucol
                                        p_fldnam    TYPE fieldname
                                        p_tabnam    TYPE tabname
                                        p_justif    TYPE char1
                                        p_seltext   TYPE dd03p-scrtext_l
                                        p_out       TYPE char1
                                        p_tech      TYPE char1
                                        p_qfield    TYPE slis_fieldname
                                        p_qtab      TYPE slis_tabname.
* Local declaration for field catalog
  DATA: wa_lfl_fcat    TYPE  slis_fieldcat_alv.
  wa_lfl_fcat-row_pos        =  p_rowpos.     "Row
  wa_lfl_fcat-col_pos        =  p_colpos.     "Column
  wa_lfl_fcat-fieldname      =  p_fldnam.     "Field Name
  wa_lfl_fcat-tabname        =  p_tabnam.     "Internal Table Name
  wa_lfl_fcat-just           =  p_justif.     "Screen Justified
  wa_lfl_fcat-seltext_l      =  p_seltext.    "Field Text
  wa_lfl_fcat-no_out         =  p_out.        "No output
  wa_lfl_fcat-tech           =  p_tech.       "Technical field
  wa_lfl_fcat-qfieldname     =  p_qfield.     "Quantity unit
  wa_lfl_fcat-qtabname       =  p_qtab .      "Quantity table
  IF p_fldnam = 'NTGEW'.
    wa_lfl_fcat-do_sum  = 'X'.
  ENDIF.
  APPEND wa_lfl_fcat TO i_fieldcat.
  CLEAR wa_lfl_fcat.
ENDFORM.                    " sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
*&      Form  sub_populate_layout
*&---------------------------------------------------------------------*
*       Populate ALV layout
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_populate_layout .
  CLEAR wa_layout.
  wa_layout-colwidth_optimize = 'X'." Optimization of Col width
ENDFORM.                    " sub_populate_layout
*&---------------------------------------------------------------------*
*&      Form  sub_populate_sort
*&---------------------------------------------------------------------*
*       Populate ALV sort table
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_populate_sort .
* Sort on material
  wa_sort-spos = '01' .
  wa_sort-fieldname = 'MATNR'.
  wa_sort-tabname = 'I_EKPO'.
  wa_sort-up = 'X'.
  wa_sort-subtot = 'X'.
  APPEND wa_sort TO i_sort .
  CLEAR wa_sort.
* Sort on plant
  wa_sort-spos = '02'.
  wa_sort-fieldname = 'WERKS'.
  wa_sort-tabname = 'I_EKPO'.
  wa_sort-up = 'X'.
  wa_sort-subtot = 'X'.
  APPEND wa_sort TO i_sort .
  CLEAR wa_sort.
ENDFORM.                    " sub_populate_sort
*&---------------------------------------------------------------------*
*&      Form  sub_get_event
*&---------------------------------------------------------------------*
*       Get ALV grid event and pass the form name to subtotal_text
*       event
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_get_event .
  CONSTANTS : c_formname_subtotal_text TYPE slis_formname VALUE
'SUBTOTAL_TEXT'.
  DATA: l_s_event TYPE slis_alv_event.
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type     = 4
    IMPORTING
      et_events       = i_event
    EXCEPTIONS
      list_type_wrong = 0
      OTHERS          = 0.
* Subtotal
  READ TABLE i_event  INTO l_s_event
                    WITH KEY name = slis_ev_subtotal_text.
  IF sy-subrc = 0.
    MOVE c_formname_subtotal_text TO l_s_event-form.
    MODIFY i_event FROM l_s_event INDEX sy-tabix.
  ENDIF.
ENDFORM.                    " sub_get_event
*&---------------------------------------------------------------------*
*&      Form  sub_alv_report_display
*&---------------------------------------------------------------------*
*       For ALV Report Display
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM sub_alv_report_display .
  DATA: l_repid TYPE syrepid .
  l_repid = sy-repid .
* This function module for displaying the ALV report
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = l_repid
      i_callback_top_of_page   = 'SUB_ALV_TOP_OF_PAGE'
      is_layout                = wa_layout
      it_fieldcat              = i_fieldcat
      it_sort = i_sort
      it_events                = i_event
      i_default                = 'X'
      i_save                   = 'A'
    TABLES
      t_outtab                 = i_ekpo
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
*    MESSAGE i000 WITH 'Error in ALV report display'(055).
  ENDIF.
ENDFORM.                    " sub_alv_report_display
*&---------------------------------------------------------------------*
*       FORM sub_alv_top_of_page
*---------------------------------------------------------------------*
*       Call ALV top of page
*---------------------------------------------------------------------*
*       No parameter
*---------------------------------------------------------------------*
FORM sub_alv_top_of_page.                                   "#EC CALLED
* To write header for the ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = i_alv_top_of_page.
ENDFORM.                    "alv_top_of_page
*&---------------------------------------------------------------------*
*&      Form  subtotal_text
*&---------------------------------------------------------------------*
*       Build subtotal text
*----------------------------------------------------------------------*
*       P_total  Total
*       p_subtot_text Subtotal text info
*----------------------------------------------------------------------*
FORM subtotal_text CHANGING
               p_total TYPE any
               p_subtot_text TYPE slis_subtot_text.
* Material level sub total
  IF p_subtot_text-criteria = 'MATNR'.
    p_subtot_text-display_text_for_subtotal
    = 'Material level total'(009).
  ENDIF.
* Plant level sub total
  IF p_subtot_text-criteria = 'WERKS'.
    p_subtot_text-display_text_for_subtotal = 'Plant level total'(010).
  ENDIF.
ENDFORM.                    "subtotal_text
 
 

 
Selection screen:
 
Output:  
 
 
 Ref: http://www.saptechnical.com/Tutorials/ALV/Subtotals/text.htm
 
 

ALV with editable F4


*&---------------------------------------------------------------*
*& Report  ZALV_EDITF4DISPLAY                                    *
*&                                                               *
*&---------------------------------------------------------------*
*& Published at SAPTechnical.COM                                 *
*&                                                               *
*&---------------------------------------------------------------*
REPORT zalv_editf4display.
*Type pools for alv
TYPE-POOLS : slis.
*structure for t582a tbale
TYPES : BEGIN OF ty_table,
        infty TYPE infty,
        pnnnn TYPE pnnnn_d,
        zrmkz TYPE dzrmkz,
        zeitb TYPE dzeitb,
        dname TYPE dianm,
         davo TYPE davo,
        davoe TYPE davoe,
        END OF ty_table.
*Structure for infotype text
TYPES : BEGIN OF ty_itext,
        infty TYPE infty,
        itext TYPE intxt,
        sprsl TYPE sprsl,
        END OF ty_itext.
*Structure for output display
TYPES : BEGIN OF ty_output,
        infty TYPE infty,
        itext TYPE intxt,
        pnnnn TYPE pnnnn_d,
        zrmkz TYPE dzrmkz,
        zeitb TYPE dzeitb,
        dname TYPE dianm,
        davo TYPE davo,
        davoe TYPE davoe,
       END OF ty_output.
*internal table and work area declarations
DATA : it_table TYPE STANDARD TABLE OF ty_table INITIAL SIZE 0,
       it_output TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,
       it_pbo TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,
       it_ittext TYPE STANDARD TABLE OF ty_itext INITIAL SIZE 0,
       wa_table TYPE ty_table,
       wa_output TYPE ty_output,
       wa_ittext TYPE ty_itext.
*Data declarations for dropdown lists for f4
DATA: it_dropdown TYPE lvc_t_drop,
      ty_dropdown TYPE lvc_s_drop,
*data declaration for refreshing of alv
      stable TYPE lvc_s_stbl.
*Global variable declaration
DATA: gstring TYPE c.
*Data declarations for ALV
DATA: c_ccont TYPE REF TO cl_gui_custom_container,         "Custom container object
      c_alvgd         TYPE REF TO cl_gui_alv_grid,         "ALV grid object
      it_fcat            TYPE lvc_t_fcat,                  "Field catalogue
      it_layout          TYPE lvc_s_layo.                  "Layout
*ok code declaration
DATA:
  ok_code       TYPE ui_func.
*initialization event
INITIALIZATION.
*start of selection event
START-OF-SELECTION.
*select the infotypes maintained
  SELECT infty
          pnnnn
          zrmkz
          zeitb
          dname
          davo
          davoe
          FROM t582a UP TO 10 ROWS
          INTO CORRESPONDING FIELDS OF TABLE it_table.
* *Select the infotype texts
  IF it_table[] IS NOT INITIAL.
    SELECT itext
             infty
             sprsl
             FROM t582s
             INTO CORRESPONDING FIELDS OF TABLE it_ittext
             FOR ALL ENTRIES IN it_table
             WHERE infty = it_table-infty
             AND sprsl = 'E'.
  ENDIF.
*Apppending the data to the internal table of ALV output
  LOOP AT it_table INTO wa_table.
    wa_output-infty = wa_table-infty.
    wa_output-pnnnn = wa_table-pnnnn.
    wa_output-zrmkz = wa_table-zrmkz.
    wa_output-zeitb = wa_table-zeitb.
    wa_output-dname = wa_table-dname.
    wa_output-davo = wa_table-davo.
    wa_output-davoe = wa_table-davoe.
* For texts
    READ TABLE it_ittext INTO wa_ittext WITH KEY infty = wa_table-infty.
    wa_output-itext = wa_ittext-itext.
    APPEND wa_output TO it_output.
    CLEAR wa_output.
  ENDLOOP.
* Calling the ALV screen with custom container
  CALL SCREEN 0600.
*On this statement double click  it takes you to the screen painter SE51.
*Enter the attributes
*Create a Custom container and name it CCONT and OK code as OK_CODE.
*Save check and Activate the screen painter.
*Now a normal screen with number 600 is created which holds the ALV grid.
* PBO of the actual screen ,
* Here we can give a title and customized menus
*create 2 buttons with function code 'SAVE' and 'EXIT'.
* GIVE A SUITABLE TITLE
*&---------------------------------------------------------------------*
*&      Module  STATUS_0600  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0600 OUTPUT.
  SET PF-STATUS 'DISP'.
  SET TITLEBAR 'ALVF4'.
ENDMODULE.                 " STATUS_0600  OUTPUT
* calling the PBO module ALV_GRID.
*&---------------------------------------------------------------------*
*&      Module  PBO  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.
*Creating objects of the container
  CREATE OBJECT c_ccont
       EXPORTING
          container_name = 'CCONT'.
*  create object for alv grid
  create object c_alvgd
  exporting
  i_parent = c_ccont.
*  SET field for ALV
  PERFORM alv_build_fieldcat.
* Set ALV attributes FOR LAYOUT
  PERFORM alv_report_layout.
  CHECK NOT c_alvgd IS INITIAL.
* Call ALV GRID
  CALL METHOD c_alvgd->set_table_for_first_display
    EXPORTING
      is_layout                     = it_layout
      i_save                        = 'A'
    CHANGING
      it_outtab                     = it_output
      it_fieldcatalog               = it_fcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDMODULE.                 " PBO  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  alv_build_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_IT_FCAT  text
*----------------------------------------------------------------------*
*subroutine to build fieldcat
FORM alv_build_fieldcat.
  DATA lv_fldcat TYPE lvc_s_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '1'.
  lv_fldcat-fieldname = 'INFTY'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 8.
  lv_fldcat-scrtext_m = 'Infotype'.
  lv_fldcat-icon = 'X'.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '2'.
  lv_fldcat-fieldname = 'PNNNN'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 15.
  lv_fldcat-scrtext_m = 'Structure'.
  lv_fldcat-icon = ''.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '3'.
  lv_fldcat-fieldname = 'ITEXT'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 60.
  lv_fldcat-scrtext_m = 'Description'.
  lv_fldcat-icon = ''.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '5'.
  lv_fldcat-fieldname = 'ZRMKZ'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 1.
  lv_fldcat-scrtext_m = 'PERIOD'.
  lv_fldcat-icon = ''.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '6'.
  lv_fldcat-fieldname = 'ZEITB'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 5.
  lv_fldcat-scrtext_m = 'Time constraint'.
  lv_fldcat-edit = 'X'.
*To avail the existing F4 help these are to
*be given in the field catalogue
  lv_fldcat-f4availabl = 'X'.
  lv_fldcat-ref_table = 'T582A'.
  lv_fldcat-ref_field = 'ZEITB'.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '7'.
  lv_fldcat-fieldname = 'DNAME'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 15.
  lv_fldcat-scrtext_m = 'Dialogmodule'.
  lv_fldcat-icon = ''.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '8'.
  lv_fldcat-fieldname = 'DAVO'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 15.
  lv_fldcat-scrtext_m = 'Start'.
  lv_fldcat-edit = 'X'.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
  lv_fldcat-row_pos   = '1'.
  lv_fldcat-col_pos   = '9'.
  lv_fldcat-fieldname = 'DAVOE'.
  lv_fldcat-tabname   = 'IT_OUTPUT'.
  lv_fldcat-outputlen = 15.
  lv_fldcat-scrtext_m = 'End'.
  lv_fldcat-icon = ''.
  APPEND lv_fldcat TO it_fcat.
  CLEAR lv_fldcat.
*To create drop down for the field 'DAVO'
* with our own f4 help
  ty_dropdown-handle = '1'.
  ty_dropdown-value = ' '.
  APPEND ty_dropdown TO it_dropdown.
  ty_dropdown-handle = '1'.
  ty_dropdown-value = '1'.
  APPEND ty_dropdown TO it_dropdown.
  ty_dropdown-handle = '1'.
  ty_dropdown-value = '2'.
  APPEND ty_dropdown TO it_dropdown.
  ty_dropdown-handle = '1'.
  ty_dropdown-value = '3'.
  APPEND ty_dropdown TO it_dropdown.
  CALL METHOD c_alvgd->set_drop_down_table
    EXPORTING
      it_drop_down = it_dropdown.
  LOOP AT it_fcat INTO lv_fldcat.
    CASE lv_fldcat-fieldname.
** To assign dropdown in the fieldcataogue
      WHEN 'DAVO'.
        lv_fldcat-drdn_hndl = '1'.
        lv_fldcat-outputlen = 15.
        MODIFY it_fcat FROM lv_fldcat.
    ENDCASE.
  ENDLOOP.
ENDFORM.                    " alv_build_fieldcat
*&---------------------------------------------------------------------*
*&      Form  alv_report_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_IT_LAYOUT  text
*----------------------------------------------------------------------*
*Subroutine for setting alv layout
FORM alv_report_layout.
  it_layout-cwidth_opt = 'X'.
  it_layout-col_opt = 'X'.
  it_layout-zebra = 'X'.
ENDFORM.                    " alv_report_layout
* PAI module of the screen created. In case we use an interactive ALV or
*for additional functionalities we can create OK codes
*and based on the user command we can do the coding.
*&---------------------------------------------------------------------*
*&      Module  PAI  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE pai INPUT.
*To change the existing values and refresh the grid
*And only values in the dropdown or in the default
*F4 can be given , else no action takes place for the dropdown
*and error is thrown for the default F4 help and font changes to red
*and on still saving, value is not changed
  c_alvgd->check_changed_data( ).
*Based on the user input
*When user clicks 'SAVE;
  CASE ok_code.
    WHEN 'SAVE'.
*A pop up is called to confirm the saving of changed data
      CALL FUNCTION 'POPUP_TO_CONFIRM'
        EXPORTING
          titlebar       = 'SAVING DATA'
          text_question  = 'Continue?'
          icon_button_1  = 'icon_booking_ok'
        IMPORTING
          answer         = gstring
        EXCEPTIONS
          text_not_found = 1
          OTHERS         = 2.
      IF sy-subrc NE 0.
*       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
*When the User clicks 'YES'
      IF ( gstring = '1' ).
        MESSAGE 'Saved' TYPE 'S'.
*Now the changed data is stored in the it_pbo internal table
        it_pbo = it_output.
*Subroutine to display the ALV with changed data.
        PERFORM redisplay.
      ELSE.
*When user clicks NO or Cancel
        MESSAGE 'Not Saved'  TYPE 'S'.
      ENDIF.
**When the user clicks the 'EXIT; he is out
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.
  CLEAR: ok_code.
ENDMODULE.                 " PAI  INPUT
*&---------------------------------------------------------------------*
*&      Form  REDISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM redisplay .
*Cells of the alv are made non editable after entering OK to save
  CALL METHOD c_alvgd->set_ready_for_input
    EXPORTING
      i_ready_for_input = 0.
*Row and column of the alv are refreshed after changing values
  stable-row = 'X'.
  stable-col = 'X'.
*REfreshed ALV display with the changed values
*This ALV is non editable and contains new values
  CALL METHOD c_alvgd->refresh_table_display
    EXPORTING
      is_stable = stable
    EXCEPTIONS
      finished  = 1
      OTHERS    = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDFORM.                    " REDISPLAY
 
 
Output:
Initial Output:
 

Default F4 HELP for the field TC
 

F4 help in the dropdown
   

After editing the values and clicking SAVE,
 

When YES, Changed Data gets SAVED and ALV is refreshed.



Nov 12, 2010

SAP Workflow Customization Settings (How to do) .. part 4

If you do not intend to work with HCM related workflows, the next step “Check Entries from HR Control Tables” is not mandatory.
After completion of the above step, we can now observe that the steps regarding “Maintain Runtime Environment” and “Maintain Definition Environment” are in green now. 


Unless you plan to use the Guided Procedures, the above configuration should be fine to work with the workflows in SAP R/3.
Now to test the workflow configuration, click on “Start verification Workflow (F5)”. We would get the following popup message: 


Now go to your SAP Inbox to check the work item (Transaction SBWP) 


Execute the work item. 

 

We can choose one of the two options to test the workflow configuration. Let us chose the first one.
You would now receive two emails in your SAP inbox:


Nov 10, 2010

SAP Workflow Customization Settings (How to do) .. part 3

Maintain Prefix Numbers
All the objects related to workflow (standard task, workflow tasks and others) are assigned a eight digit number. The prefix number is used for the first 3 digits and the rest five is automatically assigned by the system (starting with 00001). 


Select “Maintain Prefix Numbers” and click on F8. 


Click on Display<->Change.


Click on create. 


Enter a prefix number which is not allocated to any other client in the system. 


Save your entries.
Now you can observe the green tick mark beside “Maintain prefix numbers”. 


 Continue to next post...

SAP Workflow Customization Settings (How to do) .. part 2

Schedule Background Job for Missed Deadlines 

This is the job which checks if any work item has
Select this step and click on F8.
There are chances that you might encounter the following error:

To resolve this, you need to maintain the default printer in your user profile of WF-BATCH or change the printer to a valid one. Go to SWU1 and enter WF-BATCH. Navigate to tab “Defaults”.
 
 
In the above screenshot, the output device is maintained as LP01 which is not valid. Here I am modifying the same to LOCL. Also the check box “Output immediately” is marked ‘X’. 

 
 
Save your entries.
Now let us go back to transaction SWU3 and the step “Schedule Background Job for Missed Deadlines”. Click on F8. 

You would get the following screen: 

 
 
Depending on the business requirements, we can change the interval to the desired level. However 3 minutes is the minimum duration the system accepts.
Click on “Save and Schedule”. You would get the prompt for the customization request.
Now similarly schedule the  jobs – “Work items with errors” and “Condition evaluation”.
For the step “Schedule Background job for Event Queue”, click on F9 (Automatic execution).
See the screenshot below: 


Now we can observe the green tick mark beside “Maintain Runtime Environment”.  
Now let’s move to “Maintain Definition Environment”

Continue to next post...


Ref: http://www.saptechnical.com/Tutorials/Workflow/Customization/

SAP Workflow Customization Settings (How to do) .. part 1

This document details the basic workflow customization that needs to be done in any SAP system before activating the workflow definitions.
Go to transaction SWU3. <For this demo, we have used an ECC 6.0 IDES system> 

 
 
The description or the help for each of the above steps are available on the right side of the screen.
Configure RFC Destination
This can be done automatically or manually. If done automatically, as mentioned on the right side frame, the logical RFC destination WORKFLOW_LOCAL_XXX is created (if not available). XXX is the client number. Also the user WF-BATCH is also created if not already available.
The advantage of executing the above step manually is we can maintain another user and password as required.
Select the step “Configure RFC destination” and click on F9.
If you happen to get the following popup, click on Yes.   


Once the RFC destination, you can see the Green Tick mark beside “Configure RFC Destination” (as shown in the screenshot below) 


Maintain Workflow System Administrator
Since the system being used for this demo is an IDES system, some user is maintained as workflow administrator. You can change this to the desired User using this step.
Select “Maintain Workflow System Administrator” and click on F8. 


Now you can change the Userid to the desired one.
The steps “Maintain Active Plan Version”, “Classify Decision Task as General”, “Document Generation/Form Integration”, and “Maintain Time Units” are already in Green. If not in Green, select the corresponding step and click on F9. The automatic configuration would be done.



Standard SAP FTP programs

These are standard FTP programs which allows you to FTP from within SAP.
The SAPFTP.EXE should be installed on the users local hard disk before you can use these functions.
Do a check first by executing program RSFTP005. It will tell you whether it can detect the SAPFTP.EXE program.

Module Comment
RSFTP001 SAPFTP version
RSFTP002 Execute FTP Command
RSFTP003 Test
RSFTP004 FTP copy
RSFTP005 SAPFTP check
RSFTP006 FTP command list
RSFTP007 Test FB:FTP_SERVER_TO_R3 / FTP_R3_TO_SERVER
RSFTP008 Test FB:FTP_CLIENT_TO_R3 / FTP_R3_TO_CLIENT 

Displaying ALV on the Selection Screen



REPORT zalv_sel_screen . TABLES: mara. DATABEGIN OF i_alv OCCURS 0,
      matnr 
TYPE mara-matnr,
      mtart 
TYPE mara-mtart,
      matkl 
TYPE mara-matkl,
      groes 
TYPE mara-groes,
      maktx 
TYPE makt-maktx,
      
END OF i_alv. DATA: alv_container  TYPE REF TO cl_gui_docking_container. DATA: alv_grid       TYPE REF TO cl_gui_alv_grid. DATA: layout    TYPE lvc_s_layo. DATA: fieldcat  TYPE lvc_t_fcat. PARAMETERS: p_check,
            p_file.
INITIALIZATION.
  
PERFORM get_data. AT SELECTION-SCREEN OUTPUT.
  
PERFORM build_alv.

START-
OF-SELECTION.

*************************************************************
* FORM GET_DATA
*************************************************************
FORM get_data.

  
SELECT * INTO CORRESPONDING FIELDS OF TABLE i_alv
        
FROM mara
          INNER 
JOIN makt
            
ON mara~matnr = makt~matnr
                   
UP TO 100 ROWS
               
WHERE makt~spras = sy-langu.

  
SORT i_alv ASCENDING BY matnr. ENDFORM.                    "get_data
***************************************************************
*      Form  Get_Fieldcatalog - Set Up Columns/Headers
****************************************************************
FORM get_fieldcatalog.

  
DATA: ls_fcat TYPE lvc_s_fcat.
  
REFRESH: fieldcat.

  
CLEAR: ls_fcat.
  ls_fcat-reptext    = 
'Material Number'.
  ls_fcat-fieldname  = 
'MATNR'.
  ls_fcat-ref_table  = 
'I_ALV'.
  ls_fcat-outputlen  = 
'18'.
  ls_fcat-fix_column = 
'X'.
  ls_fcat-
key        = 'X'.
  ls_fcat-col_pos    = 
'1'.
  
APPEND ls_fcat TO fieldcat.

  
CLEAR: ls_fcat.
  ls_fcat-reptext    = 
'Material Type'.
  ls_fcat-fieldname  = 
'MTART'.
  ls_fcat-ref_table  = 
'I_ALV'.
  ls_fcat-outputlen  = 
'10'.
  ls_fcat-fix_column = 
'X'.
  ls_fcat-
key        = 'X'.
  ls_fcat-col_pos    = 
'2'.
  
APPEND ls_fcat TO fieldcat.

  
CLEAR: ls_fcat.
  ls_fcat-reptext    = 
'Material Group'.
  ls_fcat-fieldname  = 
'MATKL'.
  ls_fcat-ref_table  = 
'I_ALV'.
  ls_fcat-outputlen  = 
'12'.
  ls_fcat-col_pos    = 
'3'.
  
APPEND ls_fcat TO fieldcat.

  
CLEAR: ls_fcat.
  ls_fcat-reptext    = 
'Size'.
  ls_fcat-fieldname  = 
'GROES'.
  ls_fcat-ref_table  = 
'I_ALV'.
  ls_fcat-outputlen  = 
'30'.

  ls_fcat-col_pos    = 
'4'.
  
APPEND ls_fcat TO fieldcat.

  
CLEAR: ls_fcat.
  ls_fcat-reptext    = 
'Material Description'.
  ls_fcat-fieldname  = 
'MAKTX'.
  ls_fcat-ref_table  = 
'I_ALV'.
  ls_fcat-outputlen  = 
'40'.

  ls_fcat-col_pos    = 
'5'.
  
APPEND ls_fcat TO fieldcat. ENDFORM.                    "get_fieldcatalog
*&---------------------------------------------------------------------*
*&      Form  BUILD_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_alv .

  
DATA: variant TYPE  disvariant.
  
DATA: repid TYPE sy-repid.

  repid = sy-repid.

  variant-
report = sy-repid.
  variant-username = sy-uname.

  layout-zebra = 
'X'.
  layout-edit_mode = 
'X'.

  
CHECK alv_container IS INITIAL.

  
CREATE OBJECT alv_container
    
EXPORTING
      repid     = repid
      dynnr     = sy-dynnr
      side      = alv_container->dock_at_left
      extension = 
1500.

  
CREATE OBJECT alv_grid
    
EXPORTING
      i_parent = alv_container.
*  ALV Specific. Data selection.
*  Populate Field Catalog
  
PERFORM get_fieldcatalog.

  
CALL METHOD alv_grid->set_table_for_first_display
    
EXPORTING
      is_layout        = layout
      is_variant       = variant
      i_save           = 
'U'
      i_structure_name = 
'I_ALV'
    
CHANGING
      it_outtab        = i_alv[]
      it_fieldcatalog  = fieldcat[].
ENDFORM.                    " BUILD_ALV


Output: