Pages

Thursday, February 10, 2011

ALV Utility Program

Introduction – The Idea

Developers would like build the various ALV interactive Event handlers of a Report with the minimum effort and would like to devote more time on the data extraction and data display logic. At the same time Client would like to have all the Reports maintain a consistence Event pattern like Top-of-page, End-of-Page pattern and invest more time in verifying data display in ALV.

This document will help in implementing a global ALV Utility class which can be used by all Reports for displaying output in ALV format.

Developers will be able to create ALV handlers just by calling a method of this ALV Utility global class and passing the name of the Report Program. Details of the Report will be automatically picked by the Utility class.

Moreover this Class has options for enhancements and hence can be customized as per the Project need.

This Guide will demonstrate how to automate the creation of Top-Of-Page event of an ALV output.

How to reach there?

We have developed a Utility Class ZCL_ALV_HEADER_UTILITY which can be used to implement Top-of-page section of any ALV Report with minimum coding effort. This class has different methods which, if used in proper sequence, can build a Top-of-page in any customized way.

Basic steps to implement the Top-of-page logic using this Utility class:

v Create Object of this Class.

v Pass the current Report name to this Object.

v Display the Top-of-page by passing the desired Report header text to this Object.

Prerequisite:

1. Selection screen naming convention should be as per Project naming convention guideline. This will help in configuring the Utility program for generic use.

For example:

1.1. Parameter: P_

1.2. Select-options: S_

2. All the Selection screen elements should have their correspondence Text created in the Text pool of the Report program. This is crucial since the Utility class reads the Text pool of the report.

3. Report program can use either ALV with OOPs approach or ALV with Function Module approach.

Steps to use the class in an ALV OOPs Report

ALV using OOPs approach – CL_GUI_ALV_GRID

4. For events of ALV grid we always create a Local Class to handle the events. Design the Local class in the Program to handle TOP-OF-PAGE. This Local Class should have a method to write code for TOP_OF_PAGE of Class CL_GUI_ALV_GRID. Here this method is LCL_EVENT_HANDLER.

CLASS lcl_event_handler DEFINITION FINAL.
* Event receiver definitions for ALV actions
PUBLIC SECTION. METHODS:
*Handle grid Top_Of_Page
on_grid_top_of_page
FOR EVENT top_of_page OF cl_gui_alv_grid
IMPORTING
e_dyndoc_id,

CLASS lcl_event_handler IMPLEMENTATION.

* Create the Text to be dispalyed at top of page of ALV Grid Output
METHOD on_grid_top_of_page.
* top-of-page event for grid Display
PERFORM f_event_top_of_page USING e_dyndoc_id.
ENDMETHOD. "On_top_of_page

5. The FORM will have an object of the Class CL_DD_DOCUMENT. This Object has to be designed to display the TOP-OF-PAGE form. Here this object is E_DYNDOC_ID.

FORM f_event_top_of_page USING p_dyndoc_id TYPE REF TO cl_dd_document.

***********************************************************
* Begin of use of Utiltiy Class
***********************************************************

* Define Object for Utility Class
DATA: obj_sel_opt TYPE REF TO zcl_alv_header_utility.

* Create Object of Utility Class by passing the
* CL_DD_DOCUMENT object to it
CREATE OBJECT obj_sel_opt
EXPORTING
p_dyndoc_obj = p_dyndoc_id.
* Read the current Report Selection screen
CALL METHOD obj_sel_opt->read_selection_screen
EXPORTING
repid = sy-repid
.
* Add any special parameter of Selection Screen(if present)
CALL METHOD obj_sel_opt->add_special_param
EXPORTING
param_name =
'S_DATE'
param_type =
'D'
.
* Display the ALV Top of Page by passing the desired Titles
* of the Report
CALL METHOD obj_sel_opt->set_grid_top
EXPORTING
title1 =
'XXX XXXXXXXX XXX Report'
title2 =
'Test Report for Utility'
* title3 =
* title4 =
.

***********************************************************
* End of use of Utiltiy Class
***********************************************************
* populating data to html control
PERFORM f_html_header USING p_dyndoc_id.


ENDFORM. " EVENT_TOP_OF_PAGE

In the above code:

5.1. The Object for the Utility Class is built using the Constructor method. This Constructor needs a reference object of Class CL_DD_DOCUMENT, which will be used internally by the Class to build up the TOP_OF_PAGE.

The reference Object shown here is P_DYNDOC_ID. This Object has been created while creating the implementation of the local class event.

* Define Object for Utility Class
DATA: obj_sel_opt TYPE REF TO zcl_alv_header_utility.

* Create Object of Utility Class by passing the
* CL_DD_DOCUMENT object to it
CREATE OBJECT obj_sel_opt
EXPORTING
p_dyndoc_obj = p_dyndoc_id.

5.2. After the Utility Class object is created, we have to use the READ_SELECTION_SCREEN method of this class to read the Selection screen elements of our current program. This is done by simply passing the Report program name using SY-REPID system variable.

* Read the current Report Selection screen
CALL METHOD obj_sel_opt->read_selection_screen
EXPORTING
repid = sy-repid
.

5.3. If there are any Parameters or Select Options in the Selection screen which has to have special formatting while display, then the method ADD_SPECIAL_PARAM has to be used. In this method we have to pass the name of the special parameter/select-option and also pass the type of this parameter.

Presently in this example, we wanted to display S_DATE select-option of the selection screen using External Date formatting. Passing the details of the parameter to this class method, internally the class will be converting the Date entry for this select-option to external user recognized format and will display it.

* Add any special parameter of Selection Screen(if present)
CALL METHOD obj_sel_opt->add_special_param
EXPORTING
param_name =
'S_DATE'
param_type =
'D'
.

5.4. Finally call the method SET_GRID_TOP to display the top of the page. In this method interface we can define a maximum of 4 titles for the Report. These titles will be displayed in the top before the Selection parameters are listed.

* Display the ALV Top of Page by passing the desired Titles
* of the Report
CALL METHOD obj_sel_opt->set_grid_top
EXPORTING
title1 =
'XXX XXXXXXXX XXX Report'
title2 =
'Test Report for Utility'
* title3 =
* title4 =
.

Result:

Program: ZUTILITY_TEST

ALV using Function Module approach - REUSE_ALV_GRID_DISPLAY

1. Call ALV Function Module for display. Here we are supposed to call I_CALLBACK_HTML_TOP_OF_PAGE for calling Top-of-page routine.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_prog_name
i_callback_user_command = lc_user_cmd
it_fieldcat = gt_fieldcat
i_default = lc_default
I_CALLBACK_HTML_TOP_OF_PAGE =
'TOP_OF_PAGE'
TABLES
t_outtab = gt_alvrpt
EXCEPTIONS
program_error =
1
OTHERS = 2.

IF sy-subrc NE 0.
MESSAGE i000 WITH 'Error while displaying the Grid'(005).
LEAVE LIST-PROCESSING.
ENDIF.

2. The FORM for this ‘TOP_OF_PAGE’ as shown above has to be build as shown below. This FORM will be using a CL_DD_DOCUMENT to build the Top-of-page. Inside this FORM we have to call the Utility class object and the Top-of-page is displayed.

FORM TOP_OF_PAGE USING p_dyndoc_id TYPE REF TO CL_DD_DOCUMENT.

* Define Object for Utility Class
DATA: obj_sel_opt TYPE REF TO zcl_alv_header_utility.

* Create Object of Utility Class by passing the
* CL_DD_DOCUMENT object to it
CREATE OBJECT obj_sel_opt
EXPORTING
p_dyndoc_obj = p_dyndoc_id.
* Read the current Report Selection screen
CALL METHOD obj_sel_opt->read_selection_screen
EXPORTING
repid = sy-repid
.
* Add any special parameter of Selection Screen(if present)
CALL METHOD obj_sel_opt->add_special_param
EXPORTING
param_name =
'S_DATE'
param_type =
'D'
.
* Display the ALV Top of Page by passing the desired Titles
* of the Report

CALL METHOD obj_sel_opt->set_grid_top
EXPORTING
title1 =
'Scheduling Agreement or Contract vs Receipt Tracking'
* title2 =
* title3 =
* title4 =
.
ENDFORM.

The above code is similar to the Code for OOPs approach and has the same logic for displaying Top-of-page.

Result:

Program: ZUTILITY_TEST_FM

3 comments:

  1. Great information.thanks for same.

    Ultimateblogger

    SAP-edi.blogspot.com
    learnabapprogramming.blogspot.com

    ReplyDelete
  2. nice information but how to create utility class

    ReplyDelete
    Replies
    1. Utility class is a stand alone class with methods and created on Global SAP level and not on program level.

      Delete