Use Simple ALV Functions to Make Reporting Easy

*&---------------------------------------------------------------------*
*& Report  ZBC_ALV_EXAMPLE                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
* This program explains how we can use simple ALV functions to make    *
* reporting easy and looks pretty
************************************************************************
* Programmer        : Venkat Reddy           ETA                       *
* Date              : 10/02/04                                         *
************************************************************************
* Maintenance Log                                                      *
*----------------------------------------------------------------------*
* Changed By      Date         Transport#   Description                *
*------------ ---------------------------------------------------------*
* Venkat Reddy    10/02/04     EGD913575    Changed program to avoid   *
*----------------------------------------------------------------------*
REPORT  ZBC_ALV_EXAMPLE.

************************************************************************
*                 D-A-T-A  D-E-C-L-A-R-A-T-I-O-N-S                     *
************************************************************************
tables:  sflight.

**-- TYPE-POOLS Definition
**Includes the types and constants of a type group. Since the types and
*constants specified in a type group have global validity, you cannot
*use the statement within a FORM or FUNCTION.
type-pools: slis.
PARAMETERS: P_VARI LIKE DISVARIANT-VARIANT.

**-- ALV variables
*****- Field Catalog structure
data: ls_fieldcat     type slis_fieldcat_alv,    "Field Catalog list

**--- Field Catalog table
      gt_fieldcat     type slis_t_fieldcat_alv,  "Field Catalog

**--- Layout ( How you would like to see the output )
      gs_layout       type slis_layout_alv,       "List Layout

**--  Report name
      g_repid         like sy-repid,
       g_save(1)           type c,
      g_exit(1)           type c,
      g_variant           like disvariant,
      gx_variant          like disvariant.


**-- Flight Info Internal table
data: lt_sflight like sflight occurs 0 with header line.

************************************************************************
*                 C-O-N-S-T-A-N-T-S                                    *
************************************************************************

************************************************************************
*                 S-E-L-E-C-T-I-O-N  S-C-R-E-E-N                       *
************************************************************************

selection-screen begin of block a with frame title text-100.
select-options: s_carrid  for  sflight-carrid,
                s_connid  for  sflight-connid,
                s_fldate  for  sflight-fldate default sy-datum.
selection-screen end of block a .


************************************************************************
*                 I-N-I-T-I-A-L-I-Z-A-T-I-O-N                          *
************************************************************************
initialization.

  g_repid = sy-repid.

**-- Fill ALV field catalog
  perform initialize_fieldcat using gt_fieldcat[].

***-- Build Events
*  perform build_eventtab using gt_events[].
*
**-- Read the default variant
  perform initialize_variant.

************************************************************************
*                 A-T  S-E-L-E-C-T-I-O-N  S-C-R-E-E-N                  *

************************************************************************
at selection-screen on value-request for p_vari.
**-- Display all existing variants
  call function 'REUSE_ALV_VARIANT_F4'
       exporting
            is_variant = g_variant
            i_save     = g_save
       importing
            e_exit     = g_exit
            es_variant = gx_variant
       exceptions
            not_found  = 2.
  if sy-subrc = 2.
    message id sy-msgid type 'S'      number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.
    if g_exit = space.
      p_vari = gx_variant-variant.
    endif.
  endif.


************************************************************************
*                 S-T-A-R-T  O-F  S-E-L-E-C-T-I-O-N                    *
************************************************************************
start-of-selection.

**-- Read Flight information.
perform read_flight_info.

**-- Fill ALV field catalog and display report.
  if not lt_sflight[] is initial.
    perform dislay_alv_report.
  endif.

*======================================================================*
*                 FORMS / SUB ROUTINES                                 *
*======================================================================

*&---------------------------------------------------------------------*
*&      Form  initialize_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_FIELDCAT[]  text
*----------------------------------------------------------------------*
form initialize_fieldcat using l_fieldcat type slis_t_fieldcat_alv.

  clear ls_fieldcat.

* Air line
  ls_fieldcat-fieldname   = 'CARRID'.
  ls_fieldcat-key         = 'X'.
  ls_fieldcat-col_pos     = 1.
  ls_fieldcat-seltext_s   = 'Airline'.
  ls_fieldcat-seltext_l   = 'Airline'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Flight Number
  ls_fieldcat-fieldname   = 'CONNID'.
  ls_fieldcat-key         = 'X'.
  ls_fieldcat-col_pos     = 2.
  ls_fieldcat-seltext_s   = 'Flight Number'.
  ls_fieldcat-seltext_l   = 'Flight Number'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Flight date
  ls_fieldcat-fieldname   = 'FLDATE'.
  ls_fieldcat-key         = 'X'.
  ls_fieldcat-col_pos     = 3.
  ls_fieldcat-seltext_s   = 'Flight date'.
  ls_fieldcat-seltext_l   = 'Flight date'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Airfare
  ls_fieldcat-fieldname   = 'PRICE'.
  ls_fieldcat-col_pos     = 4.
  ls_fieldcat-do_sum      = 'X'.
  ls_fieldcat-seltext_s   = 'Airfare'.
  ls_fieldcat-seltext_l   = 'Airfare'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Local Currency
  ls_fieldcat-fieldname   = 'CURRENCY'.
  ls_fieldcat-col_pos     = 5.
  ls_fieldcat-seltext_s   = 'Local Currency'.
  ls_fieldcat-seltext_l   = 'Local Currency'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Plane Type
  ls_fieldcat-fieldname   = 'PLANETYPE'.
  ls_fieldcat-col_pos     = 6.
  ls_fieldcat-seltext_s   = 'Plane type'.
  ls_fieldcat-seltext_l   = 'Plane type'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Maximum capacity
  ls_fieldcat-fieldname   = 'SEATSMAX'.
  ls_fieldcat-col_pos     = 7.
  ls_fieldcat-seltext_s   = 'Max. seats'.
  ls_fieldcat-seltext_l   = 'Max. seats'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Occupied seats
  ls_fieldcat-fieldname   = 'SEATSOCC'.
  ls_fieldcat-col_pos     = 8.
  ls_fieldcat-seltext_s   = 'Seats occupied'.
  ls_fieldcat-seltext_l   = 'Seats occupied'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Total
  ls_fieldcat-fieldname   = 'PAYMENTSUM'.
  ls_fieldcat-col_pos     = 9.
  ls_fieldcat-do_sum     = 'X'.
  ls_fieldcat-seltext_s   = 'Total amount'.
  ls_fieldcat-seltext_l   = 'Total amount'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Total
  ls_fieldcat-fieldname   = 'PAYMENTSUM'.
  ls_fieldcat-col_pos     = 9.
  ls_fieldcat-do_sum     = 'X'.
  ls_fieldcat-seltext_s   = 'Total amount'.
  ls_fieldcat-seltext_l   = 'Total amount'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.


* Max. Capacity, Buss. Class
  ls_fieldcat-fieldname   = 'SEATSMAX_B'.
  ls_fieldcat-col_pos     = 10.
  ls_fieldcat-seltext_s   = 'Max.Buss.class cap.'.
  ls_fieldcat-seltext_l   = 'Max.Buss.class cap.'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Max. occupancy, Buss. Class
  ls_fieldcat-fieldname   = 'SEATSOCC_B'.
  ls_fieldcat-col_pos     = 11.
  ls_fieldcat-seltext_s   = 'Max.Bus.CL.occupied'.
  ls_fieldcat-seltext_l   = 'Max.Bus.CL.occupied'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Max. Capacity, First. Class
  ls_fieldcat-fieldname   = 'SEATSMAX_F'.
  ls_fieldcat-col_pos     = 12.
  ls_fieldcat-seltext_s   = 'Max.Buss.class cap.'.
  ls_fieldcat-seltext_l   = 'Max.Buss.class cap.'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.

* Max. occupancy, First. Class
  ls_fieldcat-fieldname   = 'SEATSOCC_F'.
  ls_fieldcat-col_pos     = 13.
  ls_fieldcat-seltext_s   = 'Max.Bus.CL.occupied'.
  ls_fieldcat-seltext_l   = 'Max.Bus.CL.occupied'.
  append ls_fieldcat to l_fieldcat.
  clear ls_fieldcat.


ENDFORM.                    " initialize_fieldcat
*&---------------------------------------------------------------------*
*&      Form  read_flight_info

*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM read_flight_info .

refresh lt_sflight.
clear   lt_sflight.

**-- Read data from SFLIGHT table
  select *
    from SFLIGHT
    into table lt_sflight
    where carrid in s_carrid
     and  connid in s_connid
     and  fldate in s_fldate.
 if sy-subrc <> 0.
   message e208(00) with text-101.
 endif.

ENDFORM.                    " read_flight_info
*&---------------------------------------------------------------------*
*&      Form  dislay_alv_report
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM dislay_alv_report .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
*   I_INTERFACE_CHECK              = ' '
*   I_BYPASSING_BUFFER             =
*   I_BUFFER_ACTIVE                = ' '
   I_CALLBACK_PROGRAM              =  g_repid
*   I_CALLBACK_PF_STATUS_SET       = ' '
*   I_CALLBACK_USER_COMMAND        = ' '
   I_STRUCTURE_NAME                =  'sflight'
*   IS_LAYOUT                      =
   IT_FIELDCAT                     =  gt_fieldcat
*   IT_EXCLUDING                   =
*   IT_SPECIAL_GROUPS              =
*   IT_SORT                        =
*   IT_FILTER                      =
*   IS_SEL_HIDE                    =
   I_DEFAULT                      = 'X'
   I_SAVE                         = 'A'
  IS_VARIANT                     = GX_VARIANT
*   IT_EVENTS                      =
*   IT_EVENT_EXIT                  =
*   IS_PRINT                       =
*   IS_REPREP_ID                   =
*   I_SCREEN_START_COLUMN          = 0
*   I_SCREEN_START_LINE            = 0
*   I_SCREEN_END_COLUMN            = 0
*   I_SCREEN_END_LINE              = 0
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER        =
*   ES_EXIT_CAUSED_BY_USER         =
  TABLES
    T_OUTTAB                       = lt_sflight
 EXCEPTIONS
   PROGRAM_ERROR                  = 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.                    " dislay_alv_report
*&---------------------------------------------------------------------*
*&      Form  initialize_variant
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM initialize_variant .
 g_save = 'A'.
  clear g_variant.
  g_variant-report = g_repid.
  gx_variant = g_variant.
  call function 'REUSE_ALV_VARIANT_DEFAULT_GET'
       exporting
            i_save     = g_save
       changing
            cs_variant = gx_variant
       exceptions
            not_found  = 2.
  if sy-subrc = 0.
    p_vari = gx_variant-variant.
  endif.

ENDFORM.                    " initialize_variant
ABAP Tips by: Venkat Reddy

ABAP Tips

Related ABAP Topics:
Table CDHDR and CDPOS Usage

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Interview Questions, Functional, Basis Administration and ABAP Programming Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.