61
ALV - ABAP List Viewer Programming The common desired features of any report are "column alignment", sorting, filtering, subtotals, totals etc. To implement these from scratch , a lot of coding effort is to be put. To avoid that we can use a concept called ABAP List Viewer (ALV). Using ALV, we can have three types of reports: 1. Simple Report 2. Block Report 3. Hierarchical Sequential Report Each of these reports provide function modules which help in producing desired output without much effort. Lets look at them in detail -

ALV Reports

Embed Size (px)

DESCRIPTION

ALV Reports

Citation preview

Page 1: ALV Reports

ALV - ABAP List Viewer ProgrammingThe common desired  features of any  report are "column alignment", sorting, filtering, subtotals, totals etc. To implement these from scratch , a lot of coding effort  is to be put. To avoid that we can use a concept called ABAP List Viewer (ALV).

Using ALV, we can have three types of reports:

1. Simple Report2. Block Report3. Hierarchical Sequential Report

Each of these reports provide function modules which help in producing desired output without much effort. Lets look at them in detail -

Page 2: ALV Reports

Simple ReportImportant function modules in these report are -

Reuse_alv_fieldcatalog_merge Reuse_alv_list_display Reuse_alv_events_get Reuse_alv_grid_display Reuse_alv_commentary_writeREUSE_ALV_FIELDCATALOG_MERGE

This function module is used to populate a field catalog which is essential to display the data in ALV.

If the output data is from a single dictionary table and all the columns are selected, then we need not exclusively create the field catalog. Its enough to mention the table name as a parameter (I_structure_name) in the REUSE_ALV_LIST_DISPLAY. But in other cases we need to create it.

Note : Fieldcatalog can be filled manually also by filling up all the required details into the internal table

Page 3: ALV Reports

Important parameters in are:

1. Export:

I_program_name : report id I_internal_tabname : the internal output table I_inclname : include or the report name where all the dynamic forms

are handled.2. Changing

ct_fieldcat : an internal table with the type SLIS_T_FIELDCAT_ALV which is declared in the type pool SLIS.

Page 4: ALV Reports

REUSE_ALV_LIST_DISPLAY

This is the function module which prints the data.

The important parameters are:

1. Export:

I_callback_program : report id I_bypassing_buffer : 'X' I_buffer_active : ' ' I_callback_pf_status_set : routine where a user can set his own pf

status or change the functionality of the existing pf status. I_callback_user_command : routine where the function codes are

handled. I_structure name : name of the dictionary table Is_Layout : structure to set the layout of the report It_fieldcat : internal table with the list of all fields and their attributes

which are to be printed (this table can be populated automatically by the function)

It_events : internal table with a list of all possible events of ALV and their corresponding form names.

2. Tables:

a. t_outtab : internal table with the data to be outputREUSE_ALV_EVENTS_GET:

Returns table of possible events for a a list type

1. Import:

Et_Events :The event table is returned with all possible CALLBACK events for the specified list type (column 'NAME'). For events to be processed by the Callback, their 'FORM' field must be filled. If the field is initialized, the event is ignored. The entry can be read from the event table, the field 'FORM' filled and the entry modified using

Page 5: ALV Reports

constants from the type pool SLIS.

2. Export:

I_list_type: 0 = simple list REUSE_ALV_LIST_DISPLAY

1 = hierarchical-sequential list  REUSE_ALV_HIERSEQ_LIST_DISPLAY

2 = simple block list REUSE_ALV_BLOCK_LIST_APPEND

3 = hierarchical-sequential block list   REUSE_ALV_BLOCK_LIST_HS_APPEND

REUSE_ALV_GRID_DISPLAY

A new function from ABAP4.6 version, to display the results in grid rather than a preview.

Parameters : same as reuse_alv_list_display

 Note:Grid cannot handle high volumes. Functions like sort, scrolling down consumes a lot of resources / time if the volume of data to be displayed is high. There is no clear cut definition such that if the amount of data is X go for list or grid  but the developer has to take a call based on his experience. If not sure, then list is the better option

Page 6: ALV Reports

REUSE_ALV_COMMENTARY_WRITE

This is used in the Top-of-page event to print the headings and other comments for the list.

Important Parameters

It_list_commentary : Internal table with the headings of the type slis_t_listheader.

This internal table has three fields:

1. Typ : 'H' - header, 'S' - selection, 'A' - action2. Key : only when typ is 'S'.3. Info : the text to be printed

Page 7: ALV Reports

Block ReportThis looks like a simple report but this report has the features of sorting and filtering only.This report is used if you have to display more than one report on the output. Technically speaking if you have multiple internal table with data to be displayed as separate blocks then we go for block report of ALV.

The important functions used for creating this report are:

REUSE_ALV_BLOCK_LIST_INIT REUSE_ALV_BLOCK_LIST_APPEND REUSE_ALV_BLOCK_LIST_DISPLAYREUSE_ALV_BLOCK_LIST_INIT

This function module is used to set the default gui status etc. The parameters are similar to the one used in reuse_alv_list_display or reuse_alv_grid_display

REUSE_ALV_BLOCK_LIST_APPEND

This function module adds the data to the block.

Important Parameters

1.Export :

is_layout : layout settings for block it_fieldcat : field catalog I_tabname : internal table name with all all possible events2.Tables :

t_outtab : internal table with output data.REUSE_ALV_BLOCK_LIST_DISPLAY

This function module display the list with data appended by the above

Page 8: ALV Reports

function.

Parameters : All the parameters are optional.

Hierarchical ReportsHierarchical display is used for displaying data that are related. Like sales order and item details. Here sales order details can be the header data whereas them items in the sales order can be the item data

The function module used for this is

REUSE_ALV_HIERSEQ_LIST_DISPLAY

Export:

I_CALLBACK_PROGRAM I_CALLBACK_PF_STATUS_SET I_CALLBACK_USER_COMMAND IS_LAYOUT It_fieldcat It_events I_tabname_header : Name of the internal table in the program

containing the output data of the highest hierarchy level. I_tabname_item : Name of the internal table in the program

containing the output data of the lowest hierarchy level. Is_keyinfo : This structure contains the header and item table field

names which link the two tables (shared key).Tables

t_outtab_header : Header table with data to be output t_outtab_item : Name of the internal table in the program containing

the output data of the lowest hierarchy level.All the definitions of internal tables, structures and constants are declared in a type-pool called SLIS. This internal table can be populated automatically by using REUSE_ALV_FIELDCATALOG_MERGE'.

Page 9: ALV Reports
Page 10: ALV Reports

Display Variants Display variants are used to set the default properties of an alv

output like sort criteria, filtering criteria, totaling and subtotaling etc Display variants can be user specific and standard (standard variants

can be used by any user ) Kind of display variants that can be saved is controlled by the

parameter i_save that is passed in function modules  reuse_alv_list_display / reuse_alv_grid_display

You can provide an option on the selection screen to select what display variant to be used

The common function modules related to selecting / validating display variants are

1. Reuse_alv_variant_default_get2. Reuse_alv_variant_f43. Reuse_alv_variant_existence

Page 11: ALV Reports

SAP ABAP ALV ReportsALV ( ABAP List Viewer ) is per-defined report format in SAP.Advantages of ALV Reports

Better look and feel. ALV report consists of some per-defined options like sort, filters, sum, downloading, print,

changing layout structure and many more.

List of Function Modules used to develop ALV reportsThe below are the list of Function Modules we use to display ALV reports in different formats.

REUSE_ALV_GRID_DISPLAY. "Display ALV grid format

REUSE_ALV_LIST_DISPLAY. "Display ALV List format

REUSE_ALV_COMMENTARY_WRITE. "Display Top of page, logo etc

REUSE_ALV_FIELDCATELOGUE_MERGE. "Used to generate field catelogue

REUSE_ALV_EVENTS_GET. "Use events in ALV

REUSE_ALV_HEIRARCHY_LIST_DISPLAY. "ALV Hierarchy display

REUSE_ALV_BLOCKED_LIST_DISPLAY. "Display blocked list

Page 12: ALV Reports

ALV Report with StructureDeveloping ALV report with Structure in SAP ABAP using function module REUSE_ALV_GRID_DISPLAY

+ -

Requirement: Develop a ALV report to display material details with all fields from MARA table for a material range input(Select-Options input).

In this requirement we have to display all fields from MARA (Material master table) in ALV format. We useREUSE_ALV_GRID_DISPLAY Function module to display ALV report.

Steps to create ALV Report with Structure .

Step1: Declare Internal table for MARA table.

Step2: Print Select-Options.

Step3: Get data from database using select statements.

Step4: Call Function Module REUSE_ALV_GRID_DISPLAY and pass structure name, program name and itab name.

TABLES : MARA. "DECLARE TABLE FOR SELECT-OPTIONS

DATA : IT_MARA TYPE TABLE OF MARA. "DECLARE INTERNAL TABLE FOR MARA

DATA : WA_MARA TYPE MARA.

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR. "PRINT SELECT-OPTIONS FOR MATNR

START-OF-SELECTION.

SELECT * FROM MARA INTO TABLE IT_MARA

WHERE MATNR IN S_MATNR .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "CALL FUNCTION MODULE TO DISPLAY ALV GRID

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID "SY-REPID IS A SYSTEM VARIABLE WHICH STORES CURRENT PROGRAM NAME

I_STRUCTURE_NAME = 'MARA'

TABLES

T_OUTTAB = IT_MARA. "PASS INTERNAL TABLE TO DISPLAY ALV FORMAT

Page 13: ALV Reports

The above report after modularization

The below one is full report after using modularization techniques.

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

*& Report ZALV_STR

*&

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

REPORT ZALV_STR.

TABLES: MARA.

DATA : IT_MARA TYPE TABLE OF MARA .

SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.

START-OF-SELECTION .

PERFORM GET_MARA_DATA . "Double click to create below form

END-OF-SELECTION .

PERFORM DISP_MARA_ALV . "Double click to create below form

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

*& Form GET_MARA_DATA

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM GET_MARA_DATA .

SELECT * FROM MARA

INTO TABLE IT_MARA

WHERE MATNR IN S_MATNR

.

Page 14: ALV Reports

ENDFORM. " GET__MARA_DATA

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

*& Form DISP_MARA_ALV

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM DISP_MARA_ALV .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

I_STRUCTURE_NAME = 'MARA'

TABLES

T_OUTTAB = IT_MARA.

ENDFORM. " DISP_MARA_ALV

Page 15: ALV Reports

ALV Report with Field CatalogLast Updated: June 2nd 2015 by Ashok Kumar Reddy

What is a field catalog in SAP ALV reports? Using field catalog and field catalog merge in SAP ALV reports

+ -

Field Catalog

Field catalog is an internal table which is used to pass list of fields to display in ALV report, we can set different properties to fields which are going to display in ALV.

Type Group

It is a data dictionary objects which contains all the reusable user-defined types.

Example for a type group is SLIS, which contains all the user-defined types for developing ALV reports.

TYPE-POOLS is a keyword which is used to assign the type-group to a ALV report .

Syntax :

TYPE-POOLS SLIS . "TO USE FIELD CATALOG WE HAVE TO INCLUDE SLIS TYPE-POOLS

DATA : TYPE SLIS_T_FIELDCAT_ALV . "INTERNAL TABLE FOR FIELD CATALOG

DATA : TYPE SLIS_FIELDCAT_ALV . " WORK AREA FOR FIELD CATLOG

Options/properties of field catalog.

WA_FCAT-COL_POS = '1' . "Specify position of a field

WA_FCAT-FIELDNAME = 'MATNR' . "Specify field name

WA_FCAT-TABNAME = 'IT_MARA' . "Specify internal table name

WA_FCAT-SELTEXT_M = 'MATERIALNO' . "Specify text to display column header

WA_FCAT-KEY = 'X' . "Specify if it is a key field

APPEND WA_FCAT TO IT_FCAT . "Append to field catalog internal table

Useful resourse Using append in SAP ABAP   and also Using Clear in SAP ABAP

Requirement: Develop a ALV report to display Material no(MATNR), Material type(MTART), Industry Sector(MBRSH) and Basic Unit Of measure(MEINS) for a range of material input (Select-Options).

Page 16: ALV Reports

To develop above report we have to use field catalog (because we have to display four fields only from MARA) and we have to pass foeld catalog parameter to Function Module REUSE_ALV_GIRD_DISPLAY.

REPORT ZSAPN_ALV_FCAT.

TABLES : MARA.

TYPE-POOLS SLIS .

TYPES : BEGIN OF TY_MARA, "User defined internal table type

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MEINS TYPE MARA-MEINS,

END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA .

DATA : WA_MARA TYPE TY_MARA .

DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV .

DATA : WA_FCAT LIKE LINE OF IT_FCAT .

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION .

PERFORM GET_DATA .

PERFORM CREATE_FCAT.

END-OF-SELECTION .

PERFORM DISP_ALV .

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

*& Form GET_DATA

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

* text

*----------------------------------------------------------------------*

Page 17: ALV Reports

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM GET_DATA .

SELECT MATNR MTART MBRSH MEINS FROM MARA

INTO TABLE IT_MARA

WHERE MATNR IN S_MATNR.

.

ENDFORM. " GET_DATA

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

*& Form DISP_ALV

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM DISP_ALV .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV

TABLES

T_OUTTAB = IT_MARA.

ENDFORM. " DISP_ALV

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

*& Form CREATE_FCAT

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

* text

*----------------------------------------------------------------------*

* --> p1 text

Page 18: ALV Reports

* <-- p2 text

*----------------------------------------------------------------------*

FORM CREATE_FCAT .

WA_FCAT-COL_POS = '1' .

WA_FCAT-FIELDNAME = 'MATNR' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'MATERIALNO' .

WA_FCAT-KEY = 'X' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '2' .

WA_FCAT-FIELDNAME = 'MTART' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'MATERIALTYPE' .

* WA_FCAT-NO_OUT = 'X' .

WA_FCAT-HOTSPOT = 'X' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .

WA_FCAT-FIELDNAME = 'MBRSH' .

WA_FCAT-REF_FIELDNAME = 'MBRSH' .

WA_FCAT-REF_TABNAME = 'MARA' .

* WA_FCAT-TABNAME = 'IT_MARA' .

* WA_FCAT-SELTEXT_M = 'INDSECTOR' .

* WA_FCAT-EDIT = 'X' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .

WA_FCAT-FIELDNAME = 'MEINS' .

WA_FCAT-TABNAME = 'IT_MARA' .

Page 19: ALV Reports

WA_FCAT-SELTEXT_M = 'MAT.UNITS' .

WA_FCAT-EMPHASIZE = 'C610'.

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT

Field Catalog Merge Not Recommanded 

Field Catalog can be generated in two ways one is manual with field catalog internal table and another one is with automatic field catalog generation...like below one but this one uses old syntax, this one is obsolete and is not recommended.

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

*& Report ZALV_WITH_FCAT

*&

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

*&

*&

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

REPORT ZALV_WITH_FCAT_MERGE.

TYPE-POOLS SLIS .

DATA : BEGIN OF I_MARA OCCURS 0,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MTART,

MBRSH LIKE MARA-MBRSH,

MEINS LIKE MARA-MEINS,

END OF I_MARA.

Page 20: ALV Reports

DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .

DATA : WA_FCAT LIKE LINE OF I_FCAT .

START-OF-SELECTION .

PERFORM GET_DATA .

PERFORM CREATE_FCAT_MERGE .

END-OF-SELECTION .

PERFORM DISP_ALV .

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

*& Form GET_DATA

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM GET_DATA .

SELECT * FROM MARA

INTO CORRESPONDING FIELDS OF TABLE I_MARA

.

ENDFORM. " GET_DATA

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

*& Form DISP_ALV

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM DISP_ALV .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

Page 21: ALV Reports

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IT_FIELDCAT = I_FCAT

TABLES

T_OUTTAB = I_MARA.

ENDFORM. " DISP_ALV

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

*& Form CREATE_FCAT_MERGE

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

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM CREATE_FCAT_MERGE .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = SY-REPID

I_INTERNAL_TABNAME = 'I_MARA'

I_INCLNAME = SY-REPID

CHANGING

CT_FIELDCAT = I_FCAT.

ENDFORM. " CREATE_FCAT_MERGE

Page 22: ALV Reports

ALV Report with layoutRequirement: Display list of materials for a material type, with all fields of out put are editable, no horizontal and vertical lines and hotspot on material no.

Input Screen

Out put Screen (all rows are editable and hotspot on material no)

Page 23: ALV Reports

Please follow previous lesson steps to create a fieldcatlog in ALV, in addition to that we will be adding additional properties hotspot, editable to out put fields using layout options.

The below is the code to add layout to ALV.

DATA : WA_LAYOUT TYPE SLIS_LAYOUT_ALV .

WA_LAYOUT-ZEBRA = 'X' .

WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X' .

WA_LAYOUT-EDIT = 'X' .

WA_LAYOUT-NO_VLINE = 'X' .

WA_LAYOUT-NO_HLINE = 'X' .

We will supply remaining options through fieldcatlog also.

Full referance code for using layout in ALV

REPORT ZSAPN_ALV_LAYOUT.

TYPE-POOLS : SLIS .

*DATA DECLARAATIONS

DATA : I_MARA TYPE TABLE OF MARA .

DATA : WA_MARA TYPE MARA .

*ALV DECLARATIONS

DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .

DATA : WA_FCAT TYPE SLIS_FIELDCAT_ALV .

DATA : V_POS TYPE I .

DATA : WA_LAYOUT TYPE SLIS_LAYOUT_ALV .

PARAMETERS : P_MTART TYPE MARA-MTART.

START-OF-SELECTION .

PERFORM GET_DATA .

PERFORM CREATE_FCAT .

PERFORM CREATE_LAYOUT.

PERFORM DISPLAY_DATA .

END-OF-SELECTION .

FORM GET_DATA .

SELECT * FROM MARA

Page 24: ALV Reports

INTO TABLE I_MARA

UP TO 100 ROWS WHERE MTART = P_MTART .

ENDFORM. " GET_DATA

FORM DISPLAY_DATA .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IS_LAYOUT = WA_LAYOUT

IT_FIELDCAT = I_FCAT

TABLES

T_OUTTAB = I_MARA.

ENDFORM. " DISPLAY_DATA

FORM DISPLAY_DATA_LIST .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_STRUCTURE_NAME = 'MARA'

TABLES

T_OUTTAB = I_MARA.

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.

ENDFORM. " DISPLAY_DATA_LIST

FORM CREATE_FCAT .

V_POS = 1 .

WA_FCAT-COL_POS = V_POS.

WA_FCAT-FIELDNAME = 'MATNR' .

WA_FCAT-SELTEXT_M = 'Material.NO' .

WA_FCAT-HOTSPOT = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

Page 25: ALV Reports

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MTART' .

WA_FCAT-SELTEXT_M = 'Material.Type' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MBRSH' .

WA_FCAT-SELTEXT_M = 'Ind.Sector' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MATKL' .

WA_FCAT-SELTEXT_M = 'Mat.Grp' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MEINS' .

WA_FCAT-SELTEXT_M = 'Units' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT

Page 26: ALV Reports

FORM CREATE_LAYOUT .

WA_LAYOUT-ZEBRA = 'X' .

WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X' .

WA_LAYOUT-EDIT = 'X' .

WA_LAYOUT-NO_VLINE = 'X' .

WA_LAYOUT-NO_HLINE = 'X' .

ENDFORM. " CREATE_LAYOUT

Page 27: ALV Reports

ALV with totals and sub totals in SAP ABAPWorking with totals and subtotals in SAP ALV reports using SAP ABAP, display totals and subtotals in ALV report

+ -

To calculate totals and sub-totals in ALV we need to sort table in ascending order and we need to set SUBTOT = 'X' of SORT in ALV.

Layout:

Layout is a structure which is used to decorate or embellish the out put of ALV Report.

Requirement: Display list of sales order for sales order range with totals and subtotals of price

Input screen

Output screen (with totals and subtotals).

Page 28: ALV Reports

Please follow previous lesson steps to create a fieldcatlog in ALV, in addition to that we will be adding additional properties hotspot, editable to out put fields using layout options.

The below code is used to display totals and subtotals in ALV.

DATA : I_SORT TYPE SLIS_T_SORTINFO_ALV .

DATA : WA_SORT LIKE LINE OF I_SORT .

WA_SORT-FIELDNAME = 'VBELN '.

WA_SORT-UP = 'X'.

WA_SORT-SUBTOT = 'X '.

APPEND WA_SORT TO I_SORT .

Page 29: ALV Reports

Full referance code for displaying totals and subtotals in ALV

REPORT ZALV_WITH_TOTALS_SUBTOT.

TYPE-POOLS SLIS .

tables : vbap.

TYPES : BEGIN OF TY_VBAP,

VBELN TYPE VBAP-VBELN,

POSNR TYPE VBAP-POSNR,

MATNR TYPE VBAP-MATNR,

NETWR TYPE VBAP-NETWR,

END OF TY_VBAP.

DATA : I_VBAP TYPE TABLE OF TY_VBAP .

DATA : WA_VBAP TYPE TY_VBAP .

DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .

DATA : WA_FCAT LIKE LINE OF I_FCAT .

DATA : I_SORT TYPE SLIS_T_SORTINFO_ALV .

DATA : WA_SORT LIKE LINE OF I_SORT .

select-options : s_vbeln for vbap-vbeln.

START-OF-SELECTION .

PERFORM GET_DATA .

PERFORM CREATE_FCAT.

PERFORM CALC_SUBTOT.

END-OF-SELECTION .

PERFORM DISP_ALV .

FORM GET_DATA .

SELECT VBELN POSNR MATNR NETWR FROM VBAP

INTO TABLE I_VBAP where vbeln in s_vbeln

.

ENDFORM. " GET_DATA

FORM DISP_ALV .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

Page 30: ALV Reports

* I_INTERFACE_CHECK = ' '

* I_BYPASSING_BUFFER = ' '

* I_BUFFER_ACTIVE = ' '

I_CALLBACK_PROGRAM = SY-REPID

* I_CALLBACK_PF_STATUS_SET = ' '

* I_CALLBACK_USER_COMMAND = ' '

* I_CALLBACK_TOP_OF_PAGE = ' '

* I_CALLBACK_HTML_TOP_OF_PAGE = ' '

* I_CALLBACK_HTML_END_OF_LIST = ' '

* I_STRUCTURE_NAME =

* I_BACKGROUND_ID = ' '

* I_GRID_TITLE =

* I_GRID_SETTINGS =

* IS_LAYOUT =

IT_FIELDCAT = I_FCAT

* IT_EXCLUDING =

* IT_SPECIAL_GROUPS =

IT_SORT = I_SORT

* IT_FILTER =

* IS_SEL_HIDE =

* I_DEFAULT = 'X'

* I_SAVE = ' '

* IS_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

* I_HTML_HEIGHT_TOP = 0

* I_HTML_HEIGHT_END = 0

Page 31: ALV Reports

* IT_ALV_GRAPHICS =

* IT_HYPERLINK =

* IT_ADD_FIELDCAT =

* IT_EXCEPT_QINFO =

* IR_SALV_FULLSCREEN_ADAPTER =

* IMPORTING

* E_EXIT_CAUSED_BY_CALLER =

* ES_EXIT_CAUSED_BY_USER =

TABLES

T_OUTTAB = I_VBAP

* EXCEPTIONS

* PROGRAM_ERROR = 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.

ENDFORM. " DISP_ALV

FORM CREATE_FCAT .

WA_FCAT-COL_POS = '1' .

WA_FCAT-FIELDNAME = 'VBELN' .

WA_FCAT-TABNAME = 'I_VBAP' .

WA_FCAT-SELTEXT_M = 'SDNO' .

WA_FCAT-KEY = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '2' .

WA_FCAT-FIELDNAME = 'POSNR' .

Page 32: ALV Reports

WA_FCAT-TABNAME = 'I_VBAP' .

WA_FCAT-SELTEXT_M = 'ITEMNO' .

* WA_FCAT-NO_OUT = 'X' .

WA_FCAT-HOTSPOT = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .

WA_FCAT-FIELDNAME = 'MATNR' .

WA_FCAT-TABNAME = 'I_VBAP' .

WA_FCAT-SELTEXT_M = 'MATERIALNO' .

* WA_FCAT-EDIT = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .

WA_FCAT-FIELDNAME = 'NETWR' .

WA_FCAT-TABNAME = 'I_VBAP' .

WA_FCAT-SELTEXT_M = 'NETPRICE' .

WA_FCAT-EMPHASIZE = 'C610'.

WA_FCAT-DO_SUM = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT

FORM CALC_SUBTOT .

WA_SORT-FIELDNAME = 'VBELN '.

WA_SORT-UP = 'X'.

WA_SORT-SUBTOT = 'X '.

APPEND WA_SORT TO I_SORT .

ENDFORM. " CALC_SUBTOT

Page 33: ALV Reports

Events in ALV reportsWhat are events in SAP ALV reports ? Working with events in ALV reports, use of events in ALV reporting SAP

+ -There are total 17 events available for ALV reporting, by using this events we can display report headers, report footers, user command, pf status etc.

We use REUSE_ALV_EVENTS_GET function module to get events and use in our report.

Steps for implementing events.

STEP1: Call the function module REUSE_ALV_EVENTS_GET and get all the events into a Internal Table(I_EVENTS).

STEP2: For each EVENT we need to provide a Sub-Routine name. So Read the EVENT from Internal Table into WorkArea and provide the Sub-Routine name.

STEP3: Finally , define the Sub-Routine and write logic.

Event definition Syntax.

*Get events using function module

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

* EXPORTING

* I_LIST_TYPE = 0

IMPORTING

ET_EVENTS = I_EVENTS .

*add perform name to a event

READ TABLE I_EVENTS INTO WA_EVENTS WITH KEY NAME = ‘' .

WA_EVENTS-FORM = '' .

MODIFY I_EVENTS FROM WA_EVENTS INDEX SY-TABIX .

* = name of the event

* is perform name to implement for particular event

Event implementation in perform

FORM .

** Add functionality for event

Page 34: ALV Reports

ENDFORM.

Using Top of page and end of list IN ALV reportsLast Updated: June 2nd 2015 by Ashok Kumar Reddy

Display top of page and end of list in ALV report, display ALV header and footer using ALV events

+ -

Call the function module REUSE_ALV_EVENTS_GET. Provide the sub routine name for top-of-page event. Implement the subroutine to display top of page . Call the function module REUSE_ALV_COMMENTARY_WRITE to display top-of-page in

ALV Finally export the events into internal table to the REUSE_ALV_GRID_DISPLAY.

REPORT ZALV_MATERIAL_TOP_OF_PAGE.

TYPE-POOLS : SLIS .

*DATA DECLARAATIONS

DATA : I_MARA TYPE TABLE OF MARA .

DATA : WA_MARA TYPE MARA .

*ALV DECLARATIONS

DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .

DATA : WA_FCAT TYPE SLIS_FIELDCAT_ALV .

DATA : V_POS TYPE I .

DATA : I_EVENTS TYPE SLIS_T_EVENT .

DATA : WA_EVENTS LIKE LINE OF I_EVENTS .

DATA : I_HEADING TYPE SLIS_T_LISTHEADER .

DATA : WA_HEADING LIKE LINE OF I_HEADING .

START-OF-SELECTION .

PERFORM GET_DATA .

PERFORM CREATE_FCAT .

Page 35: ALV Reports

PERFORM GET_EVENTS .

PERFORM DISPLAY_DATA .

END-OF-SELECTION .

FORM GET_DATA .

SELECT * FROM MARA

INTO TABLE I_MARA

UP TO 100 ROWS .

ENDFORM. " GET_DATA

FORM DISPLAY_DATA .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IT_FIELDCAT = I_FCAT

IT_EVENTS = I_EVENTS

TABLES

T_OUTTAB = I_MARA.

ENDFORM. " DISPLAY_DATA

FORM CREATE_FCAT .

V_POS = 1 .

WA_FCAT-COL_POS = V_POS.

WA_FCAT-FIELDNAME = 'MATNR' .

WA_FCAT-SELTEXT_M = 'Material.NO' .

WA_FCAT-HOTSPOT = 'X' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MTART' .

Page 36: ALV Reports

WA_FCAT-SELTEXT_M = 'Material.Type' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MBRSH' .

WA_FCAT-SELTEXT_M = 'Ind.Sector' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MATKL' .

WA_FCAT-SELTEXT_M = 'Mat.Grp' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

V_POS = V_POS 1.

WA_FCAT-COL_POS = V_POS .

WA_FCAT-FIELDNAME = 'MEINS' .

WA_FCAT-SELTEXT_M = 'Units' .

APPEND WA_FCAT TO I_FCAT .

CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT

FORM GET_EVENTS .

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

* EXPORTING

* I_LIST_TYPE = 0

Page 37: ALV Reports

IMPORTING

ET_EVENTS = I_EVENTS .

READ TABLE I_EVENTS INTO WA_EVENTS WITH KEY NAME = 'TOP_OF_PAGE' .

WA_EVENTS-FORM = 'FORM_TOP_OF_PAGE' .

MODIFY I_EVENTS FROM WA_EVENTS INDEX SY-TABIX .

***********END OF LIST

READ TABLE I_EVENTS INTO WA_EVENTS WITH KEY NAME = 'END_OF_LIST' .

WA_EVENTS-FORM = 'FORM_END_OF_LIST' .

MODIFY I_EVENTS FROM WA_EVENTS INDEX SY-TABIX .

ENDFORM. " GET_EVENTS

FORM FORM_TOP_OF_PAGE .

WA_HEADING-TYP = 'H' .

* WA_HEADING-KEY = 'H' .

WA_HEADING-INFO = 'MATERIAL MASTER REPORT' .

APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'S' .

WA_HEADING-KEY = 'USERNAME' .

WA_HEADING-INFO = SY-UNAME .

APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'A' .

WA_HEADING-KEY = 'DATE' .

WA_HEADING-INFO = SY-DATUM .

APPEND WA_HEADING TO I_HEADING .

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

Page 38: ALV Reports

IT_LIST_COMMENTARY = I_HEADING

* I_LOGO =

* I_END_OF_LIST_GRID =

* I_ALV_FORM =

.

ENDFORM . "FORM_TOP_OF_PAGE

FORM FORM_END_OF_LIST .

REFRESH I_HEADING .

CLEAR WA_HEADING .

WA_HEADING-TYP = 'S' .

WA_HEADING-INFO = 'IBM INDIA PVT.LTD' .

APPEND WA_HEADING TO I_HEADING .

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

IT_LIST_COMMENTARY = I_HEADING

I_END_OF_LIST_GRID = 'X'

.

ENDFORM . "FORM_END_OF_LIST

Page 39: ALV Reports

ALV Blocked List DisplayRequirement: Develop a blocked list ALV to display list of materials for a material type and also display material descriptions in another grid.

Analysis: For this requirement, we need to add one input parameter for material type, we need to get materials from MARA, and from MAKT and display in two list ALV`s using ALV blocked display.

Blocked list ALV is used to display multiple ALV`s on the same screen with blocks.

List of Function Modules used for blocked list ALV

REUSE_ALV_BLOCK_LIST_INIT REUSE_ALV_BLOCK_LIST_APPEND REUSE_ALV_BLOCK_LIST_DISPLAY

REUSE_ALV_BLOCK_LIST_INIT : is used to initialize blocked list ALV.

REUSE_ALV_BLOCK_LIST_APPEND : is used to add blocked list ALV's(we can add multiple).

REUSE_ALV_BLOCK_LIST_DISPLAY : is used to display blocked list ALV.

Follow the below steps to create blocked list ALVCreate a program in SE38, add data declerations

Create a report program in SE38 and add data declerations for MARA table, MAKT table.Add a input parameter for material type input.

TYPES: BEGIN OF TY_MARA, "user defined type for mara

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MATKL TYPE MARA-MATKL,

MEINS TYPE MARA-MEINS,

Page 40: ALV Reports

END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA, "mara internal table

WA_MARA TYPE TY_MARA. "mara work area

TYPES: BEGIN OF TY_MAKT, "user defined type for MAKT

MATNR TYPE MAKT-MATNR,

SPRAS TYPE MAKT-SPRAS,

MAKTX TYPE MAKT-MAKTX,

END OF TY_MAKT.

DATA : IT_MAKT TYPE TABLE OF TY_MAKT, "makt internal table

WA_MAKT TYPE TY_MAKT.

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

Build Field catalog for MARA and MAKT tables

Build field catalog for MARA and MAKT tables.

DATA : T_FCAT TYPE SLIS_T_FIELDCAT_ALV. "field catalog for MARA table

DATA : W_FCAT LIKE LINE OF T_FCAT.

DATA : T_FCAT_MAKT TYPE SLIS_T_FIELDCAT_ALV. "makt field catalog

DATA : W_FCAT_MAKT LIKE LINE OF T_FCAT_MAKT.

***build fcat for MARA

W_FCAT-COL_POS = '1'. "coloum position

W_FCAT-FIELDNAME = 'MATNR'. "column name

W_FCAT-TABNAME = 'IT_MARA'. "table

W_FCAT-REF_TABNAME = 'MARA'. "table

W_FCAT-REF_FIELDNAME = 'MATNR'. "referance field, it will show descriptions automatically

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '2'.

W_FCAT-FIELDNAME = 'MTART'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

Page 41: ALV Reports

W_FCAT-REF_FIELDNAME = 'MTART'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '3'.

W_FCAT-FIELDNAME = 'MBRSH'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MBRSH'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '4'.

W_FCAT-FIELDNAME = 'MATKL'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MATKL'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '5'.

W_FCAT-FIELDNAME = 'MEINS'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MEINS'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

***build fcat for MAKT

W_FCAT_MAKT-COL_POS = '1'.

W_FCAT_MAKT-FIELDNAME = 'MATNR'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

Page 42: ALV Reports

W_FCAT_MAKT-REF_FIELDNAME = 'MATNR'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

W_FCAT_MAKT-COL_POS = '2'.

W_FCAT_MAKT-FIELDNAME = 'SPRAS'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

W_FCAT_MAKT-REF_FIELDNAME = 'SPRAS'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

W_FCAT_MAKT-COL_POS = '3'.

W_FCAT_MAKT-FIELDNAME = 'MAKTX'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

W_FCAT_MAKT-REF_FIELDNAME = 'MAKTX'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

Initialize Blocked List ALV

Initialize blocked list ALV using Function Module REUSE_ALV_BLOCK_LIST_INIT.

* * init

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT' "initialize Block List ALV

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID.

Add lists to blocked list ALV

Add all ALV lists with field catalogs

DATA S_EVENTS TYPE SLIS_T_EVENT.

DATA S_LAYOUT TYPE SLIS_LAYOUT_ALV.

S_LAYOUT-ZEBRA = 'X'. "display zebra colours(alternate colours on rows)

**Add MARA list

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists

EXPORTING

Page 43: ALV Reports

IS_LAYOUT = S_LAYOUT "set layout

IT_FIELDCAT = T_FCAT "set field catalog

I_TABNAME = 'IT_MARA' "table

IT_EVENTS = S_EVENTS "events

* IT_SORT =

* I_TEXT = ' '

TABLES

T_OUTTAB = IT_MARA "out put table

* EXCEPTIONS

* PROGRAM_ERROR = 1

* MAXIMUM_OF_APPENDS_REACHED = 2

* OTHERS = 3

.

IF SY-SUBRC <> 0.

* Implement suitable error handling here

ENDIF.

**add MAKT list

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists

EXPORTING

IS_LAYOUT = S_LAYOUT "set layout

IT_FIELDCAT = T_FCAT_MAKT "set field catalog

I_TABNAME = 'IT_MAKT' "table

IT_EVENTS = S_EVENTS "events

* IT_SORT =

* I_TEXT = ' '

TABLES

T_OUTTAB = IT_MAKT "out put table

* EXCEPTIONS

* PROGRAM_ERROR = 1

* MAXIMUM_OF_APPENDS_REACHED = 2

* OTHERS = 3

.

IF SY-SUBRC <> 0.

Page 44: ALV Reports

* Implement suitable error handling here

ENDIF.

Display Blocked List

Display blocked list ALV using Function module REUSE_ALV_BLOCK_LIST_DISPLAY.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY' . "display blocked list

Full and Final Code to display blocked list ALV

REPORT ZSAPN_ALV_BLOCKEDLIST.

TYPES: BEGIN OF TY_MARA, "user defined type for mara

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MATKL TYPE MARA-MATKL,

MEINS TYPE MARA-MEINS,

END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA, "mara internal table

WA_MARA TYPE TY_MARA. "mara work area

DATA : T_FCAT TYPE SLIS_T_FIELDCAT_ALV. "field catalog for MARA table

DATA : W_FCAT LIKE LINE OF T_FCAT.

TYPES: BEGIN OF TY_MAKT, "user defined type for MAKT

MATNR TYPE MAKT-MATNR,

SPRAS TYPE MAKT-SPRAS,

MAKTX TYPE MAKT-MAKTX,

END OF TY_MAKT.

DATA : IT_MAKT TYPE TABLE OF TY_MAKT, "makt internal table

WA_MAKT TYPE TY_MAKT.

DATA : T_FCAT_MAKT TYPE SLIS_T_FIELDCAT_ALV. "makt field catalog

DATA : W_FCAT_MAKT LIKE LINE OF T_FCAT_MAKT.

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

START-OF-SELECTION.

Page 45: ALV Reports

SELECT MATNR

MTART

MBRSH

MATKL

MEINS FROM MARA "get MARA data

INTO TABLE IT_MARA UP TO 10 ROWS WHERE MTART = P_MTART.

IF NOT IT_MARA IS INITIAL .

SELECT MATNR

SPRAS

MAKTX FROM MAKT INTO TABLE IT_MAKT "get makt data

FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.

ENDIF.

***build fcat for MARA

W_FCAT-COL_POS = '1'. "coloum position

W_FCAT-FIELDNAME = 'MATNR'. "column name

W_FCAT-TABNAME = 'IT_MARA'. "table

W_FCAT-REF_TABNAME = 'MARA'. "table

W_FCAT-REF_FIELDNAME = 'MATNR'. "referance field, it will show descriptions automatically

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '2'.

W_FCAT-FIELDNAME = 'MTART'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MTART'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '3'.

W_FCAT-FIELDNAME = 'MBRSH'.

Page 46: ALV Reports

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MBRSH'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '4'.

W_FCAT-FIELDNAME = 'MATKL'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MATKL'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

W_FCAT-COL_POS = '5'.

W_FCAT-FIELDNAME = 'MEINS'.

W_FCAT-TABNAME = 'IT_MARA'.

W_FCAT-REF_TABNAME = 'MARA'.

W_FCAT-REF_FIELDNAME = 'MEINS'.

APPEND W_FCAT TO T_FCAT.

CLEAR W_FCAT.

***build fcat for MAKT

W_FCAT_MAKT-COL_POS = '1'.

W_FCAT_MAKT-FIELDNAME = 'MATNR'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

W_FCAT_MAKT-REF_FIELDNAME = 'MATNR'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

W_FCAT_MAKT-COL_POS = '2'.

W_FCAT_MAKT-FIELDNAME = 'SPRAS'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

Page 47: ALV Reports

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

W_FCAT_MAKT-REF_FIELDNAME = 'SPRAS'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

W_FCAT_MAKT-COL_POS = '3'.

W_FCAT_MAKT-FIELDNAME = 'MAKTX'.

W_FCAT_MAKT-TABNAME = 'IT_MAKT'.

W_FCAT_MAKT-REF_TABNAME = 'MAKT'.

W_FCAT_MAKT-REF_FIELDNAME = 'MAKTX'.

APPEND W_FCAT_MAKT TO T_FCAT_MAKT.

CLEAR W_FCAT_MAKT.

* * init

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT' "initialize Block List ALV

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID.

DATA S_EVENTS TYPE SLIS_T_EVENT.

DATA S_LAYOUT TYPE SLIS_LAYOUT_ALV.

S_LAYOUT-ZEBRA = 'X'.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists

EXPORTING

IS_LAYOUT = S_LAYOUT "set layout

IT_FIELDCAT = T_FCAT "set field catalog

I_TABNAME = 'IT_MARA' "table

IT_EVENTS = S_EVENTS "events

* IT_SORT =

* I_TEXT = ' '

TABLES

T_OUTTAB = IT_MARA "out put table

* EXCEPTIONS

* PROGRAM_ERROR = 1

* MAXIMUM_OF_APPENDS_REACHED = 2

* OTHERS = 3

Page 48: ALV Reports

.

IF SY-SUBRC <> 0.

* Implement suitable error handling here

ENDIF.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists

EXPORTING

IS_LAYOUT = S_LAYOUT "set layout

IT_FIELDCAT = T_FCAT_MAKT "set field catalog

I_TABNAME = 'IT_MAKT' "table

IT_EVENTS = S_EVENTS "events

* IT_SORT =

* I_TEXT = ' '

TABLES

T_OUTTAB = IT_MAKT "out put table

* EXCEPTIONS

* PROGRAM_ERROR = 1

* MAXIMUM_OF_APPENDS_REACHED = 2

* OTHERS = 3

.

IF SY-SUBRC <> 0.

* Implement suitable error handling here

ENDIF.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY' . "display blocked list

Page 49: ALV Reports
Page 50: ALV Reports

Interactive Report ALV+ -

Requirement: Display list of materials for a material type with below options. 1. Add a hotspot for material number. 2. Whenever user double click on any material no, it should go to MM03 transaction to display material.

To make an interactive report we use USER COMMAND event as call back user command for ALV.

Follow below steps to develop an interactive ALV.

Step1: Data decelerations.

TYPES : BEGIN OF TY_MARA, "User defined internal table type

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MEINS TYPE MARA-MEINS,

END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA ."internal table

DATA : WA_MARA TYPE TY_MARA . "work area

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

Step2: Get data from MARA

START-OF-SELECTION.

**get table data

SELECT MATNR MTART MBRSH MEINS FROM MARA "get data from MARA

INTO TABLE IT_MARA UP TO 50 ROWS

WHERE MTART = P_MTART.

Step3: Build field catalog

DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV . "field catalog table

DATA : WA_FCAT LIKE LINE OF IT_FCAT . "field catalog work area

*** build field catalogue

WA_FCAT-COL_POS = '1' . "column position

WA_FCAT-FIELDNAME = 'MATNR' . "column name

Page 51: ALV Reports

WA_FCAT-TABNAME = 'IT_MARA' . "table

WA_FCAT-SELTEXT_M = 'Material' . "Column lable

WA_FCAT-KEY = 'X' . "is a key field

WA_FCAT-HOTSPOT = 'X' . "Set hotspot for matnr

APPEND WA_FCAT TO IT_FCAT . "append to fcat

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '2' .

WA_FCAT-FIELDNAME = 'MBRSH' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Industry Sec' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .

WA_FCAT-FIELDNAME = 'MTART' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Material Type' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .

WA_FCAT-FIELDNAME = 'MEINS' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Base.Unit' .

WA_FCAT-REF_TABNAME = 'MARA' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

Step4: Display ALV

TYPE-POOLS SLIS . "SLIS type pool

**display ALV

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

Page 52: ALV Reports

I_CALLBACK_PROGRAM = SY-REPID

I_CALLBACK_USER_COMMAND = 'USER_COMMAND' "user command form

IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV

TABLES

T_OUTTAB = IT_MARA. "output table

Step4: Implement user command

**for to handle user command

FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM

RS_SELFIELD TYPE SLIS_SELFIELD.

CASE R_UCOMM.

WHEN '&IC1'. "standard Function code for double click

READ TABLE IT_MARA INTO WA_MARA INDEX RS_SELFIELD-TABINDEX.

IF SY-SUBRC = 0.

SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR. "set parameter id

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. "call transaction

ENDIF.

ENDCASE.

ENDFORM. "user_command

Final code will be

REPORT ZSAPN_ALV_INTERACTIVE.

TYPE-POOLS SLIS .

TYPES : BEGIN OF TY_MARA, "User defined internal table type

MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,

MBRSH TYPE MARA-MBRSH,

MEINS TYPE MARA-MEINS,

END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA ."internal table

DATA : WA_MARA TYPE TY_MARA . "work area

Page 53: ALV Reports

DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV . "field catalog table

DATA : WA_FCAT LIKE LINE OF IT_FCAT . "field catalog work area

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

START-OF-SELECTION.

**get table data

SELECT MATNR MTART MBRSH MEINS FROM MARA "get data from MARA

INTO TABLE IT_MARA UP TO 50 ROWS

WHERE MTART = P_MTART.

*** generate field catalogue

WA_FCAT-COL_POS = '1' . "column position

WA_FCAT-FIELDNAME = 'MATNR' . "column name

WA_FCAT-TABNAME = 'IT_MARA' . "table

WA_FCAT-SELTEXT_M = 'Material' . "Column label

WA_FCAT-KEY = 'X' . "is a key field

WA_FCAT-HOTSPOT = 'X' . "Set hotspot for matnr

APPEND WA_FCAT TO IT_FCAT . "append to fcat

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '2' .

WA_FCAT-FIELDNAME = 'MBRSH' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Industry Sec' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .

WA_FCAT-FIELDNAME = 'MTART' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Material Type' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

Page 54: ALV Reports

WA_FCAT-COL_POS = '4' .

WA_FCAT-FIELDNAME = 'MEINS' .

WA_FCAT-TABNAME = 'IT_MARA' .

WA_FCAT-SELTEXT_M = 'Base.Unit' .

WA_FCAT-REF_TABNAME = 'MARA' .

APPEND WA_FCAT TO IT_FCAT .

CLEAR WA_FCAT .

**display ALV

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

I_CALLBACK_USER_COMMAND = 'USER_COMMAND' "user command form

IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV

TABLES

T_OUTTAB = IT_MARA. "output table

**for to handle user command

FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM

RS_SELFIELD TYPE SLIS_SELFIELD.

CASE R_UCOMM.

WHEN '&IC1'. "standard Function code for doubel click

READ TABLE IT_MARA INTO WA_MARA INDEX RS_SELFIELD-TABINDEX.

IF SY-SUBRC = 0.

SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR. "set parameter id

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. "call transaction

ENDIF.

ENDCASE.

ENDFORM. "user_command