SAP Tutorials Blog




 TABLES:     vbak.


TYPE-poolsslis.                                 "ALV Declarations
TYPESBEGIN OF t_vbak,
  vbeln TYPE vbak-vbeln,
  erdat TYPE vbak-erdat,
  netwr TYPE vbak-netwr,
 CELLCOLOR TYPE LVC_T_SCOL,
END OF t_vbak.

DATAit_vbak TYPE STANDARD TABLE OF t_vbak INITIAL SIZE 0,
      wa_vbak TYPE t_vbak.

*ALV data declarations
DATAfieldcatalog 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,
      gt_events     TYPE slis_t_event,
      gd_prntparams TYPE slis_print_alv.

START-OF-SELECTION.

PERFORM fill_data.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM set_cell_colours.
PERFORM display_alv_report.

*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.

  fieldcatalog-fieldname   'VBELN'.
  fieldcatalog-seltext_m   'Sales Order'.
  fieldcatalog-col_pos     0.
  fieldcatalog-outputlen   10.
  fieldcatalog-emphasize   'X'.
  fieldcatalog-KEY         'X'.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   'ERDAT'.
  fieldcatalog-seltext_m   'Created Date  '.
  fieldcatalog-col_pos     1.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   'NETWR'.
  fieldcatalog-seltext_m   'Document Value'.
  fieldcatalog-col_pos     2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

ENDFORM.                    " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gd_layout-no_input          'X'.
  gd_layout-colwidth_optimize 'X'.
  gd_layout-totals_text       'Totals'(201).
  gd_LAYOUT-coltab_fieldname 'CELLCOLOR'.  "CTAB_FNAME
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
    i_callback_top_of_page   'TOP-OF-PAGE'  "see FORM
    is_layout               gd_layout
    it_fieldcat             fieldcatalog[]
    i_save                  'X'

  TABLES
    t_outtab                it_vbak
  EXCEPTIONS
    program_error           1
    OTHERS                  2.
  IF sy-subrc <> 0.

  ENDIF.
ENDFORM.                    " DISPLAY_ALV_REPORT

*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form vbak table and populate itab it_vbak
*----------------------------------------------------------------------*
FORM fill_data.

  SELECT vbeln erdat netwr
  UP TO 10 ROWS
  FROM vbak
  INTO CORRESPONDING FIELDS OF TABLE it_vbak.
ENDFORM.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM top-OF-PAGE.
*ALV Header declarations
  DATAt_header TYPE slis_t_listheader,
        wa_header TYPE slis_listheader,
        t_line LIKE wa_header-info,
        ld_lines TYPE I,
        ld_linesc(10TYPE C.

* Title
  wa_header-typ  'H'.
  wa_header-info 'Sales Order Report'.
  APPEND wa_header TO t_header.
  CLEAR wa_header.

* Date
  wa_header-typ  'S'.
  wa_header-KEY 'Date: '.
  CONCATENATE  sy-datum+6(2'.'
  sy-datum+4(2'.'
  sy-datum(4INTO wa_header-info.   "todays date
  APPEND wa_header TO t_header.
  CLEARwa_header.

* Total No. of Records Selected
  DESCRIBE TABLE it_vbak LINES ld_lines.
  ld_linesc ld_lines.
  CONCATENATE 'Total No. of Records Selected: ' ld_linesc
  INTO t_line SEPARATED BY space.
  wa_header-typ  'A'.
  wa_header-info t_line.
  APPEND wa_header TO t_header.
  CLEARwa_headert_line.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
  EXPORTING
    it_list_commentary t_header.
ENDFORM.                    "top-of-page


*&---------------------------------------------------------------------*
*&      Form  SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*       Set colour of individual ALV cell, field
*----------------------------------------------------------------------*
FORM SET_CELL_COLOURS .
  DATAWA_CELLCOLOR TYPE LVC_S_SCOL.
  DATAld_index TYPE SY-TABIX.

  LOOP AT IT_VBAK INTO wa_vbak.
    LD_INDEX SY-TABIX.

    WA_CELLCOLOR-FNAME 'VBELN'.
    WA_CELLCOLOR-COLOR-COL sy-tabix.
    WA_CELLCOLOR-COLOR-INT '1'.  "1 = Intensified on, 0 = Intensified off
    WA_CELLCOLOR-COLOR-INV '0'.  "1 = text colour, 0 = background colour
    APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
    MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.

    IF wa_vbak-netwr GT 0.
      WA_CELLCOLOR-FNAME 'NETWR'.
      WA_CELLCOLOR-COLOR-COL 4.
      WA_CELLCOLOR-COLOR-INT '0'.
      WA_CELLCOLOR-COLOR-INV '0'.
      APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
      MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.
    ENDIF.


    WA_CELLCOLOR-FNAME 'ERDAT'.
    WA_CELLCOLOR-COLOR-COL 6.
    WA_CELLCOLOR-COLOR-INT '0'.
    WA_CELLCOLOR-COLOR-INV '1'.
    APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
    MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.
  ENDLOOP.

ENDFORM.


Serkan AKKAVAK
Computer Engineer BSc
SAP Department Deputy Manager
Contact : serkurumsal@yandex.com