Monday, March 25, 2013

ABAP List Viewer (ALV) Reports


      Sap provides a set of ALV (ABAP LIST VIEWER) function modules, which can be put into use to embellish the output of a report. This set of ALV functions is used to enhance the readability and functionality of any report output. Cases arise in sap when the output of a report contains columns extending more than 255 characters in length. In such cases, this set of ALV functions can help choose selected columns and arrange the different columns from a report output and also save different variants for report display. This is a very efficient tool for dynamically sorting and arranging the columns from a report output. The report output can contain up to 90 columns in the display with the wide array of display options.

 
The commonly used ALV function modules  are;

 
1. REUSE_ALV_LIST_DISPLAY

2. REUSE_ALV_GRID_DISPLAY

3. REUSE_ALV_EVENTS_GET

4. REUSE_ALV_COMMENTARY_WRITE

5. REUSE_ALV_FIELDCATALOG_MERGE

 
The different steps used for getting the above function modules into use are described below.
 
Step 1: DATA DECLARATION
Sap standard type pools: SLIS.
Sap standard tables types taken from the type pools are:
 
SLIS_LAYOUT_ALV,
SLIS_T_FIELDCAT_ALV
SLIS_T_LISTHEADER,
SLIS_T_EVENT,
SLIS_SELFIELD.
 
Internal tables to be used in the program declared based on the above table types
 
TYPE-POOLS: SLIS.
 
* To pass name of the report in function module for ALV
Data: V_REPID LIKE SY-REPID.
 
* To pass the overall structure of the ALV report
Data: STRUCT_LAYOUT TYPE SLIS_LAYOUT_ALV.
 
* Internal table to capture various events in ALV
Data: I_EVENTS TYPE SLIS_T_EVENT.
 
* Table for catalog of the fields to be displayed
Data: I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
Data: X_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
 
* Internal table to mention the sort sequence
Data: IT_SORT TYPE SLIS_T_SORTINFO_ALV.
Data: X_SORT TYPE SLIS_SORTINFO_ALV.
 
* Internal table to display top of page
Data: i_list_top_of_page type slis_t_listheader.
 
* Internal table to pass data
DATA: BEGIN OF I_TAB OCCURS 0,
             MATNR LIKE MARA-MATNR,
             MBRSH LIKE MARA-MBRSH,
             END OF I_TAB.



Step2 (Defining Output Characteristics)

 PREPARING DISPLAY FIELDS CATALOG

 
A field catalog is prepared using the internal table (I_FIELDCAT) of type SLIS_T_FIELDCAT_ALV. Field catalog containing descriptions of the list output fields (usually a subset of the internal output table fields).

 
A field catalog is required for every ALV list output to add desired functionality (i.e. Key, Hotspot, Specific headings, Justify, Col. position etc) to certain fields of the output. If not mentioned specifically, then the defaults are taken. The possible values and defaults are listed below.

 
The minimal field catalog is documented below. This can be done in a routine using a local variable. The user can use the other optional parameters to assign output attributes to different fields in the output, which differ from the default.

A field catalog need not be built-up and passed explicitly only under the following conditions:

 
1. The internal table to be output has the same structure as a Data Dictionary structure which is referred to in the internal table declaration using LIKE or INCLUDE STRUCTURE. In this case the attributes of the different fields is taken directly from the table and the attributes (key fields, length, texts etc) need to state explicitly.

2. All fields in this structure are to be output

3. The structure name is passed to ALV in the parameter of the function module REUSE_ALV_LIST_DISPLAY or REUSE_ALV_GRID_DISPLAY.

 
All the values entered in the catalog are specific to the particular field whose name is entered in the fieldname FIELDNAME of the fieldcat structure. The name of the table is also entered in the corr. Fieldname TABNAME of the structure.

 
The different possible attributes are:
 

Col_pos (column position): This parameter is relevant when the fields in the output are to be different from the sequence of the fields in the internal table used for display. The parameter specifies the relative column position of the field in the list output. The column order can be changed interactively by the user. If this parameter is initial for all field catalog entries, columns appear in the internal table field sequence.

Value set: 0, 1 – 60

 
Fieldname (field name): This is the name of the internal table field for which the parameters are passed in the catalog.

Value set: internal output table field name (required parameter)

 
Tabname (internal output table): Name of the internal output table that contains the field FIELDCAT-FIELDNAME above.

 
Outputlen (column width): This parameter is used if the desired output length for a field is desired to be different from the internal output table field. For fields with a Data Dictionary link this parameter can be left initial. For fields without a Data Dictionary link (program field) the parameter must be given the value of the desired field list output length (column width).

Initial = column width is the output length of the referred Data Dictionary field (domain).

N = column width is n characters.
 

Text The following parameters are used for customizing the texts in the heading of the output of the columns. The texts are taken from the Data Dictionary for fields with a Data Dictionary reference. If this is not desired, the text parameters can also be specified. The Data Dictionary texts are then ignored.

If the user changes the column width interactively, the column header text with the appropriate length is always used.

The interactive function 'Optimize column width' takes account of both the field contents and the column headers: if all field contents are shorter than the shortest column header, the column width depends on the column header.

The 'long field label' is also used in display variant definition,

Sort, etc. Popup.

seltext_l (long field label)

 
seltext_m (medium field label)

 
seltext_s (short field label)

 Sample code to populate Fieldcatalog:

 

X_FIELDCAT-COL_POS = 0.                     * First field to appear in ALV list

X_FIELDCAT-FIELDNAME = 'MATNR'.  * Name of the field in the internal table

X_FIELDCAT-TABNAME = 'I_TAB'.        * Name of the internal table

X_FIELDCAT-OUTPUTLEN =  18.            * Output Length of the field

X_FIELDCAT-SELTEXT_M = 'MatItem'.  * Heading for the column

APPEND X_FIELDCAT TO I_FIELDCAT.

CLEAR X_FIELDCAT.

 

X_FIELDCAT-COL_POS = 1.

X_FIELDCAT-FIELDNAME = 'MBRSH'.

X_FIELDCAT-TABNAME = 'I_TAB'.

X_FIELDCAT-SELTEXT_M = 'Industry Sector'.

X_FIELDCAT-OUTPUTLEN = 50.

APPEND X_FIELDCAT TO I_FIELDCAT.

CLEAR X_FIELDCAT.


Step 3(Build up events table)

The next step is to build an event table, which are used for firing both user commands and the system dependent events.

 
 A list of possible events is populated into an event table (I_EVENTS) when this table is passed from the function module REUSE_ALV_EVENT_GET. The return table from this function module contains all the possible events.

 
The function module contains following import and export parameters.

IMPORTING PARAMETERS: I_LIST_TYPE

This parameter has possible values from 0-4.

The parameter I_LIST_TYPE is of TYPE SLIS_LIST_TYPE and is DEFAULT 0 .

EXPORTING PARAMETERS: I_EVENTS table.

This table is of TYPE SLIS_T_EVENT and returns to the program the name of all the possible events.

The table structure contains the fields:

I_EVENTS-NAME: Name of the Callback event.

I_EVENTS-FORM: Name of the form routine that should be called in the calling program at the event.

Only events with a form routine name are processed.

 
1. Slis_ev_user_command TYPE slis_formname VALUE 'USER_COMMAND'.

As this is a frequently-used Callback event, the form routine can also be passed directly in the interface by passing the user command in the IMPORTING parameter I_CALLBACK_USER_COMMAND.

 
2. Slis_ev_top_of_page TYPE slis_formname VALUE 'TOP_OF_PAGE'.

Equivalent to the list processing TOP-OF-PAGE event.

 
3. Slis_ev_top_of_coverpage TYPE slis_formname VALUE 'TOP_OF_COVERPAGE'.

The selection information and list status are output together (if they exist) on a separate page by default

 
4. Slis_ev_end_of_coverpage TYPE slis_formname VALUE 'END_OF_COVERPAGE'.

Analogously to TOP_OF_COVERPAGE the user can add other information

to the information output by ALV (selection information, list status) at this event.

 
5. Slis_ev_pf_status_set TYPE slis_formname VALUE 'PF_STATUS_SET'.

If a user list status is to be set, it must be done in the form routine assigned to this event. The ALV function codes, which must not be active, are in the Parameter RT_EXTAB. This table must be passed with the SET PF-STATUS command (with inactive user function codes as well, if necessary).

 
Sample code for Events :

FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE',

FORMNAME_USER_COMMAND TYPE SLIS_FORMNAME VALUE 'USER_COMMAND'.

 
DATA: L_I_EVENT TYPE SLIS_ALV_EVENT.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

           I_LIST_TYPE = 0

IMPORTING

          ET_EVENTS = I_EVENTS.

 
READ TABLE I_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE INTO L_I_EVENT.

 
IF SY-SUBRC = 0.

  MOVE FORMNAME_TOP_OF_PAGE TO L_I_EVENT-FORM.

  APPEND L_I_EVENT TO I_EVENTS.

ENDIF.

 
READ TABLE I_EVENTS WITH KEY NAME = SLIS_EV_USER_COMMAND INTO L_I_EVENT.

IF SY-SUBRC = 0.

  MOVE FORMNAME_USER_COMMAND TO L_I_EVENT-FORM.

  APPEND L_I_EVENT TO I_EVENTS.

ENDIF.

 
This will prepare the events table for the report.

 
The report will contain three forms for the above events:

1. FORM TOP_OF_PAGE: This form will contain the top of page event for the report i.e. header etc

Using the function module ‘REUSE_ALV_COMMENTARY_WRITE’, the internal table containing the headings for top of page event can be passed to the list output. Also, any logo specific to the report can be passed to the function module.

 
2. FORM USER_COMMAND: This form will contain the desired user command i.e. pick/line selection

 
Step 4(Report Output list description)

A layout is build for the report output list description USING the internal table declared above (I_LAYOUT).

Output list description structure.

The parameters are described under the following heads:

• Display options

• Exceptions

• Totals

• Interaction

• Detail screen

• Display variants (only for hierarchical-sequential lists)

• Color

• Other


The layout table is of type slis_layout_alv_spec and has the following fields:

Display options


1. Colwidth_optimize (1) TYPE c: This parameter optimizes the length of the different columns in the output. The width of the different col. now depends on the max. Length of the data in the column.

Value set: SPACE, 'X'

'X' = optimizes the column width so that all contents are displayed completely.


2. No_colhead (1) TYPE c: This parameter suppresses the column headings

Value set: SPACE, 'X'.

'X' = column headers are not output


3. Zebra(1) TYPE c : The report is output in the striped pattern.

Value set: SPACE, 'X'.

'X' = striped pattern (e.g. for wide lists)

 
4. No_sumchoice (1) TYPE c: This parameter allows the choice for summing up Only by field catalog.

Value set: SPACE, 'X'

'X' = fields which are to be summed, passed by the calling program (FIELDCAT-DO_SUM = 'X'). The user should not be able to change this value interactively.

 

5. List_append(1) TYPE c : no call screen. It is only useful to output block-lists without specifying the above modules if the number of list blocks exceeds, or may exceed, the maximum number specified in the block module documentation. These operations are not possible for user-defined block lists.

 
Example code for Layouts :

I_LAYOUT-zebra = 'X'.

I_LAYOUT-colwidth_optimize = 'X'.

I_LAYOUT-key_hotspot = ‘X’.

 
Step 5(Deciding Sort Criteria)

The Table IT_SORT is populated with the sort criteria for the different fields.

The caller specifies the sorting and/or subtotaling of the basic list in the internal table IT_SORT.

This internal table has the following fields:

• spos : Sort sequence

• fieldname : Internal output table field name

• tabname : Only relevant for hierarchical-sequential lists. Name of the internal output table.

• up : 'X' = sort in ascending order

• down : 'X' = sort in descending order

• subtot : 'X' = subtotal at group value change

• group : '* ' = new page at group value change ,'UL' = underline at group value change

 
Step 6(Final Step)

The final step in the output of the report is the use of two ALV functions modules.

1. REUSE_ALV_FIELDCATALOG_MERGE

2. REUSE_ALV_LIST_DISPLAY

 
The first function module is used to pass the field catalog to the report output and merge it with the internal output table.


FUNCTION reuse_alv_fieldcatalog_merge.

*"---------------------------------------------------------------------

*"*"Lokale Schnittstelle:

*” IMPORTING

*" VALUE(I_PROGRAM_NAME) LIKE SY-REPID OPTIONAL

*" VALUE(I_INTERNAL_TABNAME) TYPE SLIS_TABNAME OPTIONAL

*" VALUE(I_STRUCTURE_NAME) LIKE DD02L-TABNAME OPTIONAL

*" VALUE(I_CLIENT_NEVER_DISPLAY) TYPE SLIS_CHAR_1 default ‘X’

*" VALUE(I_INCLNAME) LIKE TRDIR-NAME OPTIONAL

*" CHANGING

*" VALUE(CT_FIELDCAT) TYPE SLIS_T_FIELDCAT_ALV

*" EXCEPTIONS

*" INCONSISTENT_INTERFACE

*" PROGRAM_ERROR

*"---------------------------------------------------------------------

Import parameters

I_PROGRAM_NAME: Program in which the internal output table is declared and populated

I_INTERNAL_TABNAME: Internal output table name

I_STRUCTURE_NAME: Structure name (structure, table, and view)

I_CLIENT_NEVER_DISPL: Hide client fields default ‘X’

I_INCLNAME: Data declarations include name

CHANGING parameter

CT_FIELDCAT: Field catalog with field descriptions

 

The variant based on a program-internal table should only be used for rapid prototyping since the following restrictions apply:

1. Performance is affected since the code of the table definition must always be read and interpreted at runtime.

2. Dictionary reference are only considered if the keywords LIKE or INCLUDE STRUCTURE (not TYPE) are used.

 

Step 7(Display Internal Output Table)

The other function module is used to display the internal output table with the contents

FUNCTION reuse_alv_list_display.

*"----------------------------------------------------------------------

*"*"Lokale Schnittstelle:

” IMPORTING

*" VALUE(I_INTERFACE_CHECK) DEFAULT SPACE

*" VALUE(I_CALLBACK_PROGRAM) LIKE SY-REPID DEFAULT SPACE

*" VALUE(I_CALLBACK_PF_STATUS_SET) TYPE SLIS_FORMNAME DEFAULT SPACE

*" VALUE(I_CALLBACK_USER_COMMAND) TYPE SLIS_FORMNAME DEFAULT SPACE

*" VALUE(I_STRUCTURE_NAME) LIKE DD02L-TABNAME OPTIONAL

*" VALUE(IS_LAYOUT) TYPE SLIS_LAYOUT_ALV OPTIONAL

*" VALUE(IT_FIELDCAT) TYPE SLIS_T_FIELDCAT_ALV OPTIONAL

*" VALUE(IT_EXCLUDING) TYPE SLIS_T_EXTAB OPTIONAL

*" VALUE(IT_SPECIAL_GROUPS) TYPE SLIS_T_SP_GROUP_ALV OPTIONAL

*" VALUE(IT_SORT) TYPE SLIS_T_SORTINFO_ALV OPTIONAL

*" VALUE(IT_FILTER) TYPE SLIS_T_FILTER_ALV OPTIONAL

*" VALUE(IS_SEL_HIDE) TYPE SLIS_SEL_HIDE_ALV OPTIONAL

*" VALUE(I_DEFAULT) DEFAULT 'X'

*" VALUE(I_SAVE) DEFAULT SPACE

*" VALUE(IS_VARIANT) LIKE DISVARIANT STRUCTURE DISVARIANT DEFAULT SPACE

*" VALUE(IT_EVENTS) TYPE SLIS_T_EVENT OPTIONAL

*" VALUE(IT_EVENT_EXIT) TYPE SLIS_T_EVENT_EXIT OPTIONAL

*" VALUE(IS_PRINT) TYPE SLIS_PRINT_ALV OPTIONAL

*" VALUE(IS_REPREP_ID) TYPE SLIS_REPREP_ID OPTIONAL

*" VALUE(I_SCREEN_START_COLUMN) DEFAULT 0

*" VALUE(I_SCREEN_START_LINE) DEFAULT 0

*" VALUE(I_SCREEN_END_COLUMN) DEFAULT 0

*" VALUE(I_SCREEN_END_LINE) DEFAULT 0

" EXPORTING

*" VALUE(E_EXIT_CAUSED_BY_CALLER)

*" VALUE(ES_EXIT_CAUSED_BY_USER) TYPE SLIS_EXIT_BY_USER

" TABLES

*" T_OUTTAB

" EXCEPTIONS

*" PROGRAM_ERROR

Import parameters

I_INTERFACE_CHECK: Interface consistency check log output.

I_CALLBACK_PROGRAM: Name of the calling program

I_CALLBACK_PF_STATUS_SET: Set EXIT routine to status.

I_CALLBACK_USER_COMMAND: EXIT routine for command handling

I_STRUCTURE_NAME: Internal output table structure name

IS_LAYOUT: List layout specifications

IT_FIELDCAT: Field catalog with field descriptions

IT_EXCLUDING: Table of inactive function codes

IT_SPECIAL_GROUPS: Grouping fields for column selection

IT_SORT: Sort criteria for first list display

IT_FILTER: Filter criteria for first list output

IS_SEL_HIDE: Selection information modification

I_DEFAULT: Initial variant active/inactive logic

I_SAVE: Variants can be saved

IS_VARIANT: Variant information

IT_EVENTS: Table of events to perform

IT_EVENT_EXIT: Standard fcode exit requests table

IS_PRINT: Print information

IS_REPREP_ID: Initialization keys for Re/Re interface

I_SCREEN_START_COLUMN: Coordinates for list in dialog box

I_SCREEN_START_LINE: Coordinates for list in dialog box

I_SCREEN_END_COLUMN: Coordinates for list in dialog box

I_SCREEN_END_LINE: Coordinates for list in dialog box

IT_EVENT_EXIT: Standard fcode exit requests table

IS_PRINT: Print information

IS_REPREP_ID: Initialization keys for Re/Re interface

I_SCREEN_START_COLUMN: Coordinates for list in dialog box

I_SCREEN_START_LINE: Coordinates for list in dialog box

I_SCREEN_END_COLUMN: Coordinates for list in dialog box

I_SCREEN_END_LINE: Coordinates for list in dialog box

Export parameters

E_EXIT_CAUSED_BY_CALLER: Delete list in CALLBACK_USER_COMMAND

ES_EXIT_CAUSED_BY_USER: How the user left the list Tables

T_OUTTAB: Table with data to be displayed ---mandatory



Documentation on function module: REUSE_ALV_GRID_DISPLAY

The function module outputs an internal table with whatever structure in the form of a formatted single- or multi-line list.

Process:

􀂾Passing an internal table with the set of information to be output

􀂾Passing a structure with general layout specifications for list layout

􀂾Passing a field catalog in the form of an internal table

 
The field catalog describes the fields to be output in the list.

Notes

􀂙All interactions performed on the list refer directly to the internal output table. Sorting the list, for example, also involves a resorting of the internal output table passed (since it was passed by reference).
 

􀂙An important factor determining the usability of the tool or of various generic functions (totals, subtotals) is the expected amount of data to be displayed.

 
Parameters :

• I_INTERFACE_CHECK

• I_BYPASSING_BUFFER

• I_BUFFER_ACTIVE

• I_CALLBACK_PROGRAM

• 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

• IT_EXCLUDING

• IT_SPECIAL_GROUPS

• IT_SORT

• IT_FILTER

• IS_SEL_HIDE

• I_DEFAULT

• I_SAVE

• IS_VARIANT

• IT_EVENTS

• IT_EVENT_EXIT

• IS_PRINT

• IS_REPREP_ID

• I_SCREEN_START_COLUMN

• I_SCREEN_START_LINE

• I_SCREEN_END_COLUMN

• I_SCREEN_END_LINE

• IT_ALV_GRAPHICS

• IT_ADD_FIELDCAT

• IT_HYPERLINK

• E_EXIT_CAUSED_BY_CALLER

• ES_EXIT_CAUSED_BY_USER

 

I_CALLBACK_PROGRAM: Name of the calling program

Program from which the function module is called and that contains the exit routines. The program should always be a report, function group, module pool or form routine pool (it should not be an include).

 
Caution: Never pass SY-REPID directly at the interface. If field SY-REPID contains the desired program name, you must absolutely assign this name to an auxiliary variable and pass this variable to the interface.

 
I_CALLBACK_PF_STATUS_SET: Set EXIT runtime to status

Passing an EXIT routine indicates to the ALV that the caller wants to set a self-defined user status. As a result, the default status of the ALV is not set. The interface of the form routine specified must be defined as follows:

FORM set_pf_status USING rt_extab TYPE slis_t_extab

Table RT_EXTAB contains the function codes that would be hidden on the standard user interface.

If the caller wants to use a self-defined user interface (for example, in order to provide additional list functions or use existing functions), we recommend that you copy standard status STANDARD from function group SALV and modify it accordingly. ALV standard function codes always start with '&'.

See also the documentation on parameter I_CALLBACK_USER_COMMAND.

If a self-defined user interface is used that includes function codes of the standard user interface, the function codes of the excluding table passed should be taken into account.

This means that the user status should generally be set as follows:

 
SET PF-STATUS user status EXCLUDING rt_extab.

Application functions can be added to excluding table rt_extab if they are to be disabled.

The routine is called whenever the standard user interface would be set with SET PF-STATUS.

 
I_CALLBACK_TOP_OF_PAGE

EXIT routine for handling TOP-OF-PAGE

Description

If the caller specifies an EXIT routine, this routine must have the following form:

FORM top_of_page.

Module REUSE_ALV_COMMENTARY_WRITE can then be called within the EXIT routine. This module is responsible for formatting the header information and also ensures online HTML formatting. In the print preview or in batch mode, the text passed is then output in the normal format.

If module REUSE_ALV_COMMENTARY_WRITE cannot be used, you must use two parameters instead. In I_CALLBACK_TOP_OF_PAGE you pass the form routine that is responsible for normal formatting in batch mode or in the print preview mode. The form routine that is responsible for online formatting, is passed in parameter

 
Note the section on pre-defined settings.

Display options

colwidth_optimize
 

Value range: SPACE, 'X'

X' = Optimizes the column width to ensure that the content is displayed completely.

no_colhead

Value range: SPACE, 'X'

'X' = Do not output column headings.

 
zebra

Value range: SPACE, 'X'

'X' = Striped pattern (for wide lists, for example)

 
no_vline

Value range: SPACE, 'X'

‘X' = Separate columns by SPACE.

 

You can generate the field catalog automatically or semi-automatically by calling function module

REUSE_ALV_FIELDCATALOG_MERGE.

See also the documentation on function module : REUSE_ALV_FIELDCATALOG_MERGE.

The minimum values required for the field catalog are documented in the 'Default' section. The caller can optionally use all other parameters to assign non-standard output attributes to a field.

 
It is only in the following cases that you are not required to generate the field catalog and pass it explicitly:

• The structure of the internal table to be output corresponds to a structure stored in the Data Dictionary and is referenced with LIKE or INCLUDE STRUCTURE in the declaration of the internal table.

 
• All fields of this structure should be output in the list.
 
• The structure name is declared to the ALV using parameter I_STRUCTURE_NAME.

 See also the documentation on IMPORTNG parameter I_STRUCTURE_NAME.

Positioning

col_pos (column position)

 Value range: 0, 1 - 60

Only relevant if the relative column positions should by default not be identical to the sequence of the fields in the field catalog.

The parameter determines the relative column position of the field in the list output. The column sequence can interactively be changed by the user. If this parameter is set to its initial value for each field catalog entry, the columns are arranged in the order of the fields in the field catalog.

• fieldname (field name)
 
outputlen (column width)  

Value range: 0 (initial), n

For fields with reference to the Data Dictionary you can leave this parameter set to initial.

For fields without reference to the Data Dictionary (program fields) you must set the parameter to the desired field output length on the list (column width).

initial = The column width is derived from the output length of the referenced field (domain) in the Data Dictionary.

n = The column width is n characters.  

• do_sum (calculate totals for column)
 

Value range: SPACE, 'X'

'X' = Totals are calculated for this field of the internal output table.

This function can also be used interactively by the user.

• no_sum (totals calculation not allowed)  

Value range: SPACE, 'X'

'X' = No totals may be calculated for this field although the data type of the field allows totalling.

Formatting column contents

The long field label is also used in the dialog boxes for defining the display variant, the sort order, and so on.

• seltext_l (long field label)  

• seltext_m (medium field label)  

• seltext_s (short field label)  

• reptext_ddic (heading) 
 

Example Code

WS_REPNAME = SY-REPID.
 

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = WS_REPNAME

I_INTERNAL_TABNAME = Internal output table field name

I_INCLNAME = WS_REPNAME

CHANGING

CT_FIELDCAT = I_FIELDTAB.

IF SY-SUBRC <> 0.

WRITE: 'SY-SUBRC: ', SY-SUBRC, 'REUSE_ALV_FIELDCATALOG_MERGE'.

ENDIF.

 

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = WS_REPNAME

I_STRUCTURE_NAME = Internal output table field name

IS_LAYOUT = I_LAYOUT

IT_FIELDCAT = I_FIELDTAB

I_DEFAULT = 'A'

I_SAVE = 'A'

IS_VARIANT = 'X'

IT_EVENTS = I_EVENTS[]

IT_SORT = I_SORT

IS_SEL_HIDE = I_SELINFO

TABLES

T_OUTTAB = Internal output table field name.

IF SY-SUBRC <> 0.

WRITE: 'SY-SUBRC: ', SY-SUBRC, 'REUSE_ALV_LIST_DISPLAY'.

ENDIF

 
Using other function module 'REUSE_ALV_GRID_DISPLAY’ can help us get list output in the form of a grid and also attach logos to the report output.

 
1 Simple list output:

REPORT Y_DEMO_ALV NO STANDARD PAGE HEADING.

* Data to be displayed

DATA: I_SFLIGHT TYPE TABLE OF SFLIGHT.

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

* Selection

SELECT * FROM SFLIGHT INTO TABLE I_SFLIGHT.

* Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_STRUCTURE_NAME = 'SFLIGHT'

TABLES

T_OUTTAB = I_SFLIGHT.

 

2.Simple grid output:

REPORT Y_DEMO_ALV_1.

*

* Data to be displayed

DATA: I_SFLIGHT TYPE TABLE OF SFLIGHT.

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

* Selection

SELECT * FROM SFLIGHT INTO TABLE I_SFLIGHT.

* Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_STRUCTURE_NAME = 'SFLIGHT'

TABLES

T_OUTTAB = I_SFLIGHT.

 
 

Wednesday, March 13, 2013

Enhancements (User Exits and BADI)


Enhancements

SAP has developed its various modules with standards, which are broadly practiced all over. But yet the requirements of customers differ from place to place. In this scenario it becomes imperative to modify SAP objects to suit the customer’s needs.

 
Hence SAP allows you to change the system accordingly and add your own functionality to the SAP system. There are four different ways of changing the SAP system to fit our needs:

 
1. Customizing: Customizing constitutes changing the system parameters with its own special user interface. These changes are organized and preplanned. It is an obligatory part of a SAP implementation process.

 
2. Enhancement Concept: It constitutes changing of SAP Repository objects by the customer without modification

 
3. Modification: Modifying the SAP repository objects in the form custom changes. This should be avoided because when SAP changes occur in new versions then the customer version and the new SAP version have to be reconciled manually. This means an increased maintenance workload because of the need to adjust all such modifications. Hence, use of this option should be avoided as far as possible.

 
4. Customer Development: If the customer requirements are not met by SAP then we should go in for customer development. These means that we need to create customer specific objects within the customer range. Modification and Customer Development involve high maintenance and costs. Hence use these only when customer requirements are not met by customizing or by exits. SAP creates customer exits for specific programs, screens and menus within standard R/3 applications.

 
There are two methods in enhancements.

    1. User Exits.
    2. BADI (Business Add Ins)


User Exits


There are mainly two reasons to why we have to use exits if we ever have to.

 
1. You should them because they do not affect the SAP source code but still allow you to change the functionality of SAP to suit your needs. SAP has provided us with some standard exits that we should modify by adding our encapsulated as separate objects. When you create Exits, they are encapsulated as separate objects.

 
2. And since, they do not affect the source code and are named as per the SAP naming conventions; they do affect future software upgrades. Hence you do not have to save them and then reenter add-ons attached to exits. You can only use exits if they already exist within the SAP R/3 System In case you do not find an exit available for an area where you would want to make a change, then you should request SAP to develop an exit.

 
There are four types of exits:

1. Menu Exits

2. Screen Exits

3. Function module Exits

4. Field Exit

 
1. Menu Exits

Using the Menu Exits you can add menu items to the menus of standard R/3 Applications. The Menu Exit entries have function codes that begin with + (plus sign).

 
2. Function Exits

This includes creating function modules with the administrative part, the interface and the documentation but usually without any additional code. Function module exits play a role in both in menu and screen exits When you add a new menu item to a standard pull-down menu, for example, you can use a function module exit.

 
3. Screen Exits

Screen Exits defines special areas called Subscreens within a screen. Using Screen Exits you can add subscreens to the screens within R/3 applications.

 
4. FIELD EXITS

Let us now see how to create field exits.

Ø  First and foremost, ask your system administrator to include the parameter abap/fieldexit = Y in your Instance Profile

Ø  For the Field Exits we do not need an Enhancement project.

Ø  To create a Field Exit, you need to first determine which Data Element you            would want to change, The program that you would like to have the effect on, The screen number.

 
In our example we will consider the transaction MM02, which is for changing the material master. We will select the Basic Data view and then make fields exit for restricting the Basic Unit Of Measurement. For example, we will not allow the user to enter ‘BOX’ as basic unit of measurement. For this field, we need to have details such the Data Element, Program name and Screen Number.

 This information can be gathered by as follows:

i. Start the Transaction MM02.

ii. Select a material and click enter

iii. From the pop up that you get, select the Basic Data view

iv. Locate the label Base Unit of Measure in the General Data section.

v. Place your cursor in the field and press F1

vi. In the Help screen of this field, press on Technical Info button and note down the above information


BADI

Detailed explanation about BADI with an example 


Defination: 

 BADI (Business Add-In) is a new SAP Object Oriented enhancement technique which is used to add our own business functionality to the existing SAP standard functionality. BADI's are available in SAP R/3 from the system release 4.6c

 Why BADI?
In contrast to the earlier enhancement techniques, BADI follows Object Oriented approach to make them reusable. A BADI can be used any number of times where as standard enhancement techniques can be used only once.

For example if we assign an enhancement to one custom project, then that enhancement cannot be assigned to any other custom projects. To overcome this drawback SAP has provided a new enhancement technique called BADI.

 Transaction code for BADI Definition:  SE18
When you create a BAdI definition, a class interface will be automatically created and you can define your methods in the interface. The implementation of the methods can be done in SE19 transaction.

When a BAdi is created following are automatically generated:An interface with 'IF_EX_' inserted between the first and second characters of the BAdi name. An adapter class with 'CL_EX_' inserted between the first and second characters of the BAdi name.

 Transaction code to Implement BADI:  SE19

 Types of BADI's:

 While creating a BADI using the T-code SE18, it provides the pop-up screen to select the type of BADI to be used is as shown below.
 
 


There are two types of BADI's.  

1) Multi use BADI:

With this option, any number of active implementations can be assigned to the same definition BADI. By default this option is checked.If we want the BADI for multiple use

If you have multiple-use BADI definitions, the sequence must not play any role.

The drawback in Multiple use BADI is, it is not possible to know which BADI is active especially in country specific version.

2) Filter dependent BADI:

Using this option we can define the BADI's according to the filter values to control the add-in implementation on specific criteria.

Ex: Specific country value.

 How to Find BADI's in SAP system:

Method 1:

 Steps to find BADI: 

1. Go to SE 24 transaction, type CL_EXITHANDLER and then click on display.
 

2. Double click on GET_INSTANCE method.



3.Put a  break-point on             class                  method                                                           CL_EXITHANDLER=>GET_CLASS_NAME_BY_INTERFACE.


 

4. Run any transaction on which we want finds the BADI's say VA01. 

5. Give the transaction name VA01 and press enter.


6. It will automatically take you to the break-point which we have set the in the SE24   transaction. Each time if you press F8 a list BADI names will be displayed


7. You can find the BADI name in field EXIT_NAME and if you double click on it, we can get the corresponding BADI name before hit the corresponding screen. Based on the requirement find the BADI name and accordingly implement your functionality using the transaction se19.

Method 2:

Go to transaction SE84 and click on Enhancements. Then double click on Business Add-Ins. For example if you want to find the BADI's for the standard transaction ME22n, the procedure is as follows. This example shows, finding the way of BADI names by providing the Package of ME22n. 


1)      Go to transaction ME22n. Select the System option from the menu and then click on Status. It displays the following information.



 

2)       Double click on the program name i.e. SAPLMEGUI. It will take you into the program and click on Go to tab from the Menu. There we can find the package name of the standard transaction ME22n.Copy and paste it in the package filed.



3)      Now Press F8, a list of BADI names will be displayed as shown below. Select the appropriate BADI name and implement it based on the business requirement using the transaction SE19.

 
Method 3:

Finding the BADI names using SE18 transaction
1)  Go to transaction SE 18. Select F4 help for the definition name and click on Information System button as shown below

 
 
2). A pop-up screen will be displayed and give the package name for any standard transaction say VA02. Finding the package is explained above. Please refer above method to find the package name. The package name for VA02 transaction is 'VA.'
 

 
3)      A list of BADI names will be displayed for the transaction VA02. Select the appropriate BADI name and implement it using T-code SE19.
 
Example :

This Example explains how to implement BADI's. Here I am trying to show how to add custom screen to the ME23N Transactions using BADI's.
The procedure is as explained below. 
-         Find the BADI using the method 1 as shown above.
The BADI name to add the custom screen to ME23n is 'ME_GUI_PO_CUST'.

-   Go to T-code SE19 and create an implementation ZME_GUI_PO_CUST and then click on create button 

-   Give the Definition name "ME_GUI_PO_CUST" and continue, give short text, save
-    Click on Interface Tab, you can find the implementation class name as ZCL_IM_ME_GUI_PO_CUST2
-    Click on ZCL_IM_ME_GUI_PO_CUST2, which will take you to SE24.
-    Add "MMMFD" in the Type Group Section of properties tab. 
-    Go to Attributes section and declare the following attribute
-    SUBSCREEN1 Constant Public Type MEPO_NAME Name of a View
'ITEMSCREEN1 '.
-         Go to Methods section, you can find the BADI interface methods. 
-          Double click on the method "IF_EX_ME_GUI_PO_CUST~SUBSCRIBE", this method is having 3 parameters

Go to the code section of the method and add the following code there. 
Code for IF_EX_ME_GUI_PO_CUST~SUBSCRIBE:

  DATA: ls_subscriber LIKE LINE OF re_subscribers.
*--FIRST SCREEN POPULATION
*--we want to add a customer subscreen on the item detail tab
  CHECK im_application = 'PO'.
  CHECK im_element     = 'ITEM'.
*--each line in re_subscribers generates a subscreen. We add one subscreen
*--in this example
  CLEAR re_subscribers[].
*--the name is a unique identifier for the subscreen and defined in this
*--class definition
  ls_subscriber-name = subscreen1.
*--the dynpro number to use
  ls_subscriber-dynpro = '0002'.
*--the program where the dynpro can be found
  ls_subscriber-program = 'ZME_GUI_PO_CUST_SCREEN'.
*--each subscreen needs itsown DDIC-Structure
  ls_subscriber-struct_name = 'ZMARA'.
*--a label can be defined
  ls_subscriber-label = 'Cust BADI'.
*--the position within the tabstrib can be defined
  ls_subscriber-position = 7.
*--the height of the screen can be defined here. Currently we suport two
*--screen sizes:
*--value <= 7 a sevel line subscreen
*--value > 7 a 16 line subscreen
  ls_subscriber-height = 7.
  APPEND ls_subscriber TO re_subscribers.
Save and check & back
Double click on method IF_EX_ME_GUI_PO_CUST~MAP_DYNPRO_FIELDS".
Add the following code in the method.
*given the field catalog of structure ZMARA we have to
*establish a mapping to metafields which are used for field selection
*purposes and error handling Standard definitions can be found in type
*pool MMMFD. It is important for customer fields to use integer
*constants above 90000000 for the metafield.
  FIELD-SYMBOLS: LIKE LINE OF ch_mapping.
  LOOP AT ch_mapping ASSIGNING .
    CASE -fieldname.
      WHEN 'MATNR'.      -metafield = mmmfd_cust_08.
      WHEN 'MTART'.      -metafield = mmmfd_cust_09.
      WHEN 'MATKL'.      -metafield = mmmfd_cust_10.
    ENDCASE.
  ENDLOOP. 
 
The metafield mapping important for field selection & error handling purpose.
Save, check and back
Activate the Implementation class.
Activate the BADI Implementation. 
Now create a structure in SE11 with the name ZMARA.
Add the following fields in the structure.

Now Create a Program with the Name 'ZME_GUI_PO_CUST_SCREEN' and create a screen with sub screen type with the number 0002.

Add the fields on to screen from ZMARA program 'ZME_GUI_PO_CUST_SCREEN'.
 
De comments the PBO module in screen flow logic & create the module in above program.  
Add the following code in program ZME_GUI_PO_CUST_SCREEN.
TABLES: ZMARA.
DATA: call_subscreen TYPE sy-dynnr,
      call_prog TYPE sy-repid,
      call_view TYPE REF TO cl_screen_view_mm,
      call_view_stack TYPE REF TO cl_screen_view_mm OCCURS 0.
 *---------------------------------------------------------------------*
*       FORM SET_SUBSCREEN_AND_PROG                  *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
*  -->  DYNNR                                                                     *
*  -->  PROG                                                                        *
*  -->  VIEW                                                                         *
*  -->  TO                                                                             *
*  -->  CL_SCREEN_VIEW_MM                                      *
*--------------------------------------------------------------------*
FORM set_subscreen_and_prog USING dynnr TYPE sy-dynnr
                                  prog TYPE sy-repid
                                  view TYPE REF TO cl_screen_view_mm.
  call_subscreen = dynnr.
  call_prog = prog.
  call_view = view.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0002  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0002 OUTPUT.
SELECT SINGLE * FROM MARA
          INTO CORRESPONDING FIELDS OF ZMARA
          WHERE MATNR = '100-100'. 
ENDMODULE.                 " STATUS_0002  OUTPUT  
 
The sub-routine "set_subscreen_and_prog" is must with the same signature. 
This routine is called from BADI to call the sub screen. 
Activate the screen & program. 
Now Go to T-Code ME23N to test the application. 
You can find a new tab is added in the item level with the name CUST BADI.
 
 Final Output