ABAP Code: ALV Report using FM ALV_GRID_DISPLAY

Last week, I received an email from one of our readers requesting a simple report tutorial using ALV GRID. Well, a report using an ALV is a simple one for an experienced ABAPer but for a fresher like him, it might be a complicated task. I understand him as I do became a fresher years back. When I am starting to learn ABAP, I have a senior ABAPer also who guided me as I developed. Thanks a lot to Master Jojit for his kindness and support during my greenhorn days.

For this tutorial our intended readers would be the beginners. To begin with, I summarize three major steps to display an ALV grid which I always do.
1. Inputs from Selection Screen
2. SELECT from Database Tables
3. Display using FM ALV_GRID_DISPLAY.


Input from Selection Screen
When creating a report, the first to consider is the Screen. A screen is where the user inputs data. Everything you see in SAP windows has a screen. There are two types of screens. The first one is the SELECTION-SCREEN and the second one is DIALOG SCREEN. There's a big difference between the two screens. One thing I can say is, in DIALOG SCREEN, you are the one designing the whole screen. There is a layout view in which you can position every component. If you are familiar with Visual Basic 6 or VB.net, this is similar with Forms. You can position the components like radio buttons, labels, textbox, dropdown list etc. anywhere from the screen.

Unlike SELECTION-SCREEN, this is not that case. When you declare components on it, it is vertically positioned unless you will implicitly declared to be aligned horizontally. That's one of the major difference of the two. Components positioning. I will not discuss further all the differences between these two types of screen. But I hope I give you a simple difference so that you have an idea what to use when you will develop your first report program.

Normally, when creating an ALV report, we are using the SELECTION-SCREEN. The PARAMETER and SELECT-OPTIONS are usually used here as input parameters. PARAMETER accepts single values while SELECT-OPTIONS accept multiple and complex values. Checkboxes and radiobuttons can also be declared here even a push button. For more information about selection screen, try this link.

Example on how to define a selection screen:
1
2
3
4
5
6
  SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
  SELECT-OPTIONS carrid FOR sflight-carrid.
  SELECT-OPTIONS connid FOR sflight-connid.
  SELECT-OPTIONS fldate FOR sflight-fldate.
  PARAMETERS: p_maxrow TYPE i DEFAULT 30.
  SELECTION-SCREEN END OF BLOCK b01.

SELECT from Database tables
In this step, you are going to do a retrieval process from tables, custom or standard as long as it is maintained in the data dictionary (SE11). Normally, SQL statements is called at the event START-OF-SELECTION. This event is triggered when the user clicks the execute button.  For more information about events in selection-screen, click this link.

By the way, there are two methods in executing SQL statements. The first one is OPEN SQL and the second is, NATIVE SQL. If you are familiar with other databases like mySQL, MSSQL, ORACLE and other databases in the market, they are using the NATIVE SQL. In SAP, it is also possible to use such SQL statements. However, this is not being recommended as ABAP programs having database-specific SQL statements do not run with different databases. So if your company decides to change from one database to another, there might be a problem in the future with your custom ABAP codes.

So as an ABAP developer, SAP highly recommend to use the OPEN SQL. It is a subset of the standard SQL statements which allows you to access tables regardless of manufacturers. So in the long run, no worries on your ABAP programs.

Example of and OPEN SQL statement:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 SELECT * INTO CORRESPONDING FIELDS OF TABLE gi_sflight FROM
  ( sflight LEFT JOIN scarr
  ON sflight~carrid = scarr~carrid )
  UP TO p_maxrow ROWS
  WHERE sflight~carrid IN carrid
  AND   sflight~connid IN connid
  AND   sflight~fldate IN fldate.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.



Display using FM ALV_GRID_DISPLAY
This is the last step before you can display your ALV Grid. But beforehand, you need to do the following steps first. 

a. Build the Fieldcatalog - In ABAP lingo, FieldCatalog are simply the columns. If you compare it to an Excel sheet, these are the fields with letters, "A", "B", "C", etc. In building your fieldcatalog, you will be declaring all the fields to be visible in your ALV grid which depends on the design of the functional consultant. Also, you can control which columns comes next to each other. Likewise, you can also add colors to columns, set columns to fix and many more functionality. 

b. Create the Layout - in this procedure, you are just declaring how your ALV grid looks like. In this step, you will be declaring if rows appears like zebra. Also, you can optimize also the columns width.

c. Create the PF_STATUS - in ABAP language, "PF_Status" simply means collections of events to perform  an action. So all buttons you see in your SAP screen like the Save button, Exit button, and other buttons you can click with your mouse has a corresponding PF_Status code.

d. Call FM ALV_GRID_DISPLAY - the last step before you can display your ALV is calling this function module, ALV_GRID_DISPLAY. Actually, there are two ways to display a grid type of ALV. Besides using this function module, you can also use the OOP way. But in this tutorial, I will not tackle the OOP method but I will discuss it in a separate tutorial

OOP method to display ALV Grid
1
 CALL METHOD go_grid->set_table_for_first_display



So I hope, I have given an insights all ABAP freshers an introductory on how to create their first report using an FM ALV_GRID_DISPLAY. Thank you once again, and you may connect me in my page @ABAPTHINKERS. Enjoy everyone.

Complete Sample Code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
************************************************************************
* Copyright 2013, ABAP Thinkers
* All Rights Reserved
************************************************************************
* Program ID     : ZLEVI
* Program Name   : ALV Report using ALV_GRID_DISPLAY
* Create Date    : 11.14.2013
* Created by     : ABAP Thinkers
* Request No.    : local
* Description    : This program creates an ALV report using the FM
*                  ALV_GRID_DISPLAY
************************************************************************

REPORT  zlevi.
************************************************************************
********************START OF DATA DECLARATIONS**************************
************************************************************************
*----------------------------------------------------------------------*
*TYPE-POOLS                                                            *
*----------------------------------------------------------------------*
TYPE-POOLS: abap,slis.

*----------------------------------------------------------------------*
* TABLES                                                               *
*----------------------------------------------------------------------*
TABLES:sflight.

*----------------------------------------------------------------------*
* TYPES                                                                *
*----------------------------------------------------------------------*
TYPES: BEGIN OF ty_sflight.
        INCLUDE STRUCTURE sflight.
TYPES:    add1,
          carrname LIKE scarr-carrname,
          box,
          lights.
TYPES: END OF ty_sflight.

*----------------------------------------------------------------------*
*INTERNAL TABLES                                                       *
*----------------------------------------------------------------------*
DATA:
   gi_sflight               TYPE STANDARD TABLE OF ty_sflight,
   gi_fieldcatalog          TYPE slis_t_fieldcat_alv."

*----------------------------------------------------------------------*
*WORK AREAS                                                            *
*----------------------------------------------------------------------*
DATA:
   gwa_sflight              TYPE ty_sflight,
   gwa_layout               TYPE slis_layout_alv,
   gwa_fieldcatalog         TYPE slis_fieldcat_alv.

************************************************************************
**********************END OF DATA  DECLARATIONS*************************
************************************************************************

**----------------------------------------------------------------------*
**INITIALIZATION                                                        *
**----------------------------------------------------------------------*
INITIALIZATION.
  PERFORM f_initialize.

*----------------------------------------------------------------------*
*SELECTION SCREENS                                                     *
*----------------------------------------------------------------------*
  SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
  SELECT-OPTIONS carrid FOR sflight-carrid.
  SELECT-OPTIONS connid FOR sflight-connid.
  SELECT-OPTIONS fldate FOR sflight-fldate.
  PARAMETERS: p_maxrow TYPE i DEFAULT 30.
  SELECTION-SCREEN END OF BLOCK b01.

*----------------------------------------------------------------------*
*START OF SELECTION                                                    *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM f_selection.
  PERFORM f_display_alv_report.

*&---------------------------------------------------------------------*
*&      Form  f_selection
*&---------------------------------------------------------------------*
*       Select Statement
*----------------------------------------------------------------------*
FORM f_selection.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gi_sflight FROM
  ( sflight LEFT JOIN scarr
  ON sflight~carrid = scarr~carrid )
  UP TO p_maxrow ROWS
  WHERE sflight~carrid IN carrid
  AND   sflight~connid IN connid
  AND   sflight~fldate IN fldate.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.

ENDFORM.                    "SELECTION

*&--------------------------------------------------------------*
*&      Form  f_create_layout
*&--------------------------------------------------------------*
*       Create Layout of the ALV Grid
*---------------------------------------------------------------*
FORM f_create_layout USING p_gd_layout TYPE slis_layout_alv.

  p_gd_layout-zebra             = abap_true.
  p_gd_layout-colwidth_optimize = abap_true.

ENDFORM. " f_create_layout

*&--------------------------------------------------------------*
*&      Form  pf_status
*&--------------------------------------------------------------*
*       Set the PF Status of the ALV Grid
*---------------------------------------------------------------*
FORM pf_status USING rt_extab TYPE slis_t_extab.."    "#EC CALLED
  SET PF-STATUS  'STANDARD_FULLSCREEN'  OF PROGRAM 'SAPLKKBL'.
ENDFORM.                    "pfstatus

*&--------------------------------------------------------------*
*&      Form  f_display_alv_report
*&--------------------------------------------------------------*
*       Display ALV Report
*---------------------------------------------------------------*
FORM f_display_alv_report.

  PERFORM f_create_layout USING gwa_layout.
  PERFORM f_build_fieldcatalog.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PF_STATUS'
      is_layout                = gwa_layout
      it_fieldcat              = gi_fieldcatalog[]
    TABLES
      t_outtab                 = gi_sflight[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc = 0.                                          "#EC NEEDED
    "do nothing
  ENDIF.

ENDFORM.                    " F_DISPLAY_ALV_REPORT

*&--------------------------------------------------------------*
*&      Form  f_build_fieldcatalog
*&--------------------------------------------------------------*
*       Generate the Field Catalogs of the ALV Grid
*----------------------------------------------------------------*
FORM f_build_fieldcatalog.

  REFRESH: gi_fieldcatalog.
  CLEAR: gwa_fieldcatalog.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-cprog
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = gi_fieldcatalog
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc = 0.
    "do nothing
  ENDIF.

ENDFORM.                    "f_build_fieldcatalog

*&---------------------------------------------------------------------*
*&      Form  F_INITIALIZE
*&---------------------------------------------------------------------*
*       Initialize variables
*----------------------------------------------------------------------*
FORM f_initialize .

  REFRESH:
     gi_sflight,
     gi_fieldcatalog.

  CLEAR:
     gwa_sflight,
     gwa_layout,
     gwa_fieldcatalog.
ENDFORM.                    " F_INITIALIZE

No comments:

Post a Comment