Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
284 views10 pages

SAP ALV Report Guide

This document contains code for displaying ALV reports in ABAP. It defines data structures and internal tables to store report data and field catalog information. It includes forms to retrieve data from a database table, build the field catalog, and display the ALV report by calling the REUSE_ALV_GRID_DISPLAY function module, passing the data and field catalog. It also shows how to define a top-of-page event to add a header to the report.

Uploaded by

rajeshkothamasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
284 views10 pages

SAP ALV Report Guide

This document contains code for displaying ALV reports in ABAP. It defines data structures and internal tables to store report data and field catalog information. It includes forms to retrieve data from a database table, build the field catalog, and display the ALV report by calling the REUSE_ALV_GRID_DISPLAY function module, passing the data and field catalog. It also shows how to define a top-of-page event to add a header to the report.

Uploaded by

rajeshkothamasu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ALV Reports

Normal Alv Report


TYPE-POOLS: slis. " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook TYPE TABLE OF sbook.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*Fetch data from the database


SELECT * FROM sbook INTO TABLE it_sbook.

*Build field catalog


wa_fieldcat-fieldname = 'CARRID'. " Fieldname in the data table
wa_fieldcat-seltext_m = 'Airline'. " Column description in the output
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-seltext_m = 'Con. No.'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-seltext_m = 'Book. ID'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'PASSNAME'.
wa_fieldcat-seltext_m = 'Passenger Name'.
APPEND wa_fieldcat TO it_fieldcat.

*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.

Using top of page in ALV

TABLES : SFLIGHT.
TYPE-POOLS : SLIS.
**INTERNAL TABLE DECLARTION
DATA : WA_SFLIGHT TYPE SFLIGHT,
IT_SFLIGHT TYPE TABLE OF SFLIGHT.
**DATA DECLARTION
DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
GD_LAYOUT TYPE SLIS_LAYOUT_ALV,
GD_REPID LIKE SY-REPID,
G_SAVE TYPE C VALUE 'X',
G_VARIANT TYPE DISVARIANT,
GX_VARIANT TYPE DISVARIANT,
G_EXIT TYPE C,
ISPFLI TYPE TABLE OF SPFLI.
* To understand the importance of the following parameter, click here.
**SELECTION SCREEN DETAILS
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002 .
PARAMETERS: VARIANT LIKE DISVARIANT-VARIANT.
SELECTION-SCREEN END OF BLOCK B1.
**GETTING DEFAULT VARIANT
INITIALIZATION.
GX_VARIANT-REPORT = SY-REPID.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
I_SAVE = G_SAVE
CHANGING
CS_VARIANT = GX_VARIANT
EXCEPTIONS
NOT_FOUND = 2.
IF SY-SUBRC = 0.
VARIANT = GX_VARIANT-VARIANT.
ENDIF.
**PERFORM DECLARATIONS
START-OF-SELECTION.
PERFORM DATA_RETRIVEL.
PERFORM BUILD_FIELDCATALOG.
PERFORM DISPLAY_ALV_REPORT.
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM BUILD_FIELDCATALOG .
FIELDCATALOG-FIELDNAME = 'CARRID'.
FIELDCATALOG-SELTEXT_M = 'Airline Code'.
FIELDCATALOG-COL_POS = 0.
APPEND FIELDCATALOG TO FIELDCATALOG.
CLEAR FIELDCATALOG.
FIELDCATALOG-FIELDNAME = 'CONNID'.
FIELDCATALOG-SELTEXT_M = 'Flight Connection Number'.
FIELDCATALOG-COL_POS = 1.
APPEND FIELDCATALOG TO FIELDCATALOG.
CLEAR FIELDCATALOG.
FIELDCATALOG-FIELDNAME = 'FLDATE'.
FIELDCATALOG-SELTEXT_M = 'Flight date'.
FIELDCATALOG-COL_POS = 2.
APPEND FIELDCATALOG TO FIELDCATALOG.
CLEAR FIELDCATALOG.
FIELDCATALOG-FIELDNAME = 'PRICE'.
FIELDCATALOG-SELTEXT_M = 'Airfare'.
FIELDCATALOG-COL_POS = 3.
FIELDCATALOG-OUTPUTLEN = 20.
APPEND FIELDCATALOG TO FIELDCATALOG.
CLEAR FIELDCATALOG.
ENDFORM. " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT .
GD_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = GD_REPID
I_CALLBACK_TOP_OF_PAGE = 'TOP-OF-PAGE' "see FORM
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
IT_FIELDCAT = FIELDCATALOG[]
I_SAVE = 'X'
IS_VARIANT = G_VARIANT
TABLES
T_OUTTAB = IT_SFLIGHT
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. "DISPLAY_ALV_REPORT
" DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*& Form DATA_RETRIVEL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DATA_RETRIVEL .
SELECT * FROM SFLIGHT INTO TABLE IT_SFLIGHT.
ENDFORM. " DATA_RETRIVEL
*-------------------------------------------------------------------*
* Form TOP-OF-PAGE *
*-------------------------------------------------------------------*
* ALV Report Header *
*-------------------------------------------------------------------*
FORM TOP-OF-PAGE.
*ALV Header declarations
DATA: T_HEADER TYPE SLIS_T_LISTHEADER,
WA_HEADER TYPE SLIS_LISTHEADER,
T_LINE LIKE WA_HEADER-INFO,
LD_LINES TYPE I,
LD_LINESC(10) TYPE C.
* Title
WA_HEADER-TYP = 'H'.
WA_HEADER-INFO = 'SFLIGHT Table Report'.
APPEND WA_HEADER TO T_HEADER.
CLEAR WA_HEADER.
* Date
WA_HEADER-TYP = 'S'.
WA_HEADER-KEY = 'Date: '.
CONCATENATE SY-DATUM+6(2) '.'
SY-DATUM+4(2) '.'
SY-DATUM(4) INTO WA_HEADER-INFO. "todays date
APPEND WA_HEADER TO T_HEADER.
CLEAR: WA_HEADER.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = T_HEADER.
ENDFORM. "top-of-page

Interactive Report

This program displays the Purchase Order header details on the basic list and on double-clicking any of the
record on the basic list, the item-level information is displayed on the secondary list.

REPORT ZPURCHASE_ORDER.

***********************************************************************
* TYPE-POOLS DECLARATION
***********************************************************************
TYPE-POOLS:
SLIS.

***********************************************************************
* DATA DECLARATIONS
***********************************************************************

DATA:
W_EBELN TYPE EKKO-EBELN,
W_PROG TYPE SY-REPID,
T_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
FS_FIELDCAT LIKE LINE OF T_FIELDCAT,
T_EVENTCAT TYPE SLIS_T_EVENT,
W_EVENTCAT LIKE LINE OF T_EVENTCAT.

***********************************************************************
* SELECT-OPTIONS DECLARATION
***********************************************************************
SELECT-OPTIONS:
S_EBELN FOR W_EBELN.

***********************************************************************
* INTERNAL TABLE AND FIELD-STRING DECLARATIONS
***********************************************************************

DATA:
T_EKKO LIKE
STANDARD TABLE
OF EKKO,
FS_EKKO LIKE LINE OF T_EKKO.
DATA:
T_EKPO LIKE
STANDARD TABLE
OF EKPO,
FS_EKPO LIKE LINE OF T_EKPO.

***********************************************************************
* START-OF-SELECTION
***********************************************************************
START-OF-SELECTION.
SELECT *
FROM EKKO
INTO TABLE T_EKKO
WHERE EBELN IN S_EBELN.

W_PROG = SY-REPID.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'


EXPORTING
I_CALLBACK_PROGRAM = W_PROG
I_CALLBACK_USER_COMMAND = 'PICK'
I_STRUCTURE_NAME = 'EKKO'
TABLES
T_OUTTAB = T_EKKO
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*&--------------------------------------------------------------------*
*& Form pick
*&--------------------------------------------------------------------*
* -->UCOMM text
* -->SELFIELD text
*---------------------------------------------------------------------*
FORM PICK USING COMMAND LIKE SY-UCOMM
SELFIELD TYPE SLIS_SELFIELD.
READ TABLE T_EKKO INTO FS_EKKO INDEX SELFIELD-TABINDEX.

CASE COMMAND.
WHEN '&IC1'.
SELECT *
FROM EKPO
INTO TABLE T_EKPO
WHERE EBELN EQ FS_EKKO-EBELN.

W_PROG = SY-REPID.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'


EXPORTING
I_STRUCTURE_NAME = 'EKPO'
CHANGING
CT_FIELDCAT = T_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

DELETE T_FIELDCAT WHERE FIELDNAME EQ 'EBELN'.


DELETE T_FIELDCAT WHERE FIELDNAME EQ 'BUKRS'.
DELETE T_FIELDCAT WHERE FIELDNAME EQ 'LGORT'.
DELETE T_FIELDCAT WHERE FIELDNAME EQ 'WERKS'.

Perform t_eventcat.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'


EXPORTING
I_CALLBACK_PROGRAM = W_PROG
IT_FIELDCAT = T_FIELDCAT
IT_EVENTS = T_EVENTCAT
TABLES
T_OUTTAB = T_EKPO
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDCASE. " CASE COMMAND


ENDFORM. " FORM PICK

FORM T_EVENTCAT.
W_EVENTCAT-NAME = 'TOP_OF_PAGE'.
W_EVENTCAT-FORM = 'TOP'.
APPEND W_EVENTCAT TO T_EVENTCAT.
ENDFORM.

FORM TOP.
READ TABLE T_EKPO INTO FS_EKPO INDEX 1.
WRITE:/ 'Purchase Document Number'(001),30 FS_EKPO-EBELN,
/ 'Company Code'(002), 30 FS_EKPO-BUKRS,
/ 'Plant'(003), 30 FS_EKPO-WERKS,
/ 'Storage Location'(004),30 FS_EKPO-LGORT.
ENDFORM.
Interactive ALV

REPORT zalv_tcoderefresh.
*type pools for alv declarations
TYPE-POOLS: slis.
*structure declaration for tstc table
TYPES : BEGIN OF ty_tstc,
tcode TYPE tcode,
pgmna TYPE program_id,
dypno TYPE dynpronr,
END OF ty_tstc.
* Internal table and workarea declarations for tstc
DATA: it_tstc TYPE STANDARD TABLE OF ty_tstc,
wa_tstc TYPE ty_tstc.
*data declarations for ALV
DATA: it_layout TYPE slis_layout_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
it_fieldcat TYPE slis_t_fieldcat_alv,
it_eventexit TYPE slis_t_event_exit,
wa_eventexit TYPE slis_event_exit.
*initialisation event
INITIALIZATION.
*start of selection event
START-OF-SELECTION.
*subroutine to fetch data from the db table
PERFORM fetch_data.
*subroutine for output display
PERFORM alv_output.
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
* *subroutine to fetch data from the db table
*----------------------------------------------------------------------*
FORM fetch_data.
*Internal table and work area declaratin for TSTC (local tables)
DATA : lt_tstc TYPE STANDARD TABLE OF ty_tstc,
ls_tstc TYPE ty_tstc.
*Static field definition
*Reads the last tcode and stores it in l_tstc that on refresh further data
*beyond this value is fetched
STATICS l_tstc TYPE tcode.
* Selection from the tstc table
*we select till 25 rows and on further refresh next 25 are selected
*we select transactions having screen numbers only
SELECT tcode
pgmna
dypno
FROM tstc
INTO CORRESPONDING FIELDS OF TABLE lt_tstc
UP TO 25 ROWS
WHERE tcode GT l_tstc
AND dypno NE '0000'.
* Code for transferring the values of local table to output table
* for 25 rows as sy-tfill is 25.
*In case there are no records a message pops up.
IF sy-subrc EQ 0.
DESCRIBE TABLE it_tstc.
READ TABLE lt_tstc INTO ls_tstc INDEX sy-tfill.
l_tstc = ls_tstc-tcode.
it_tstc[] = lt_tstc[].
ELSE.
MESSAGE 'No Records found ' TYPE 'i'.
ENDIF.
ENDFORM. "read_data
*&---------------------------------------------------------------------*
*& Form alv_output
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM alv_output.
*subroutine to refresh alv
PERFORM event_exits.
*field catalogue
PERFORM build_fieldcat.
*Layout for alv
PERFORM build_layout.
*output display
PERFORM alv_display.
ENDFORM. "alv_output
*&---------------------------------------------------------------------*
*& Form event_exits
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*subroutine to refresh alv
FORM event_exits.
CLEAR wa_eventexit.
wa_eventexit-ucomm = '&REFRESH'. " Refresh
wa_eventexit-after = 'X'.
APPEND wa_eventexit TO it_eventexit.
ENDFORM. "event_exits
*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*Field catalogue
FORM build_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-row_pos = '1'.
wa_fieldcat-col_pos = '1'.
wa_fieldcat-fieldname = 'TCODE'.
wa_fieldcat-tabname = 'it_tstc'.
wa_fieldcat-seltext_m = 'TRANSACTION'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-row_pos = '1'.
wa_fieldcat-col_pos = '2'.
wa_fieldcat-fieldname = 'PGMNA'.
wa_fieldcat-tabname = 'it_tstc'.
wa_fieldcat-seltext_m = 'PROGRAM'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-row_pos = '1'.
wa_fieldcat-col_pos = '3'.
wa_fieldcat-fieldname = 'DYPNO'.
wa_fieldcat-tabname = 'it_tstc'.
wa_fieldcat-seltext_m = 'SCREEN'.
APPEND wa_fieldcat TO it_fieldcat.
ENDFORM. "build_fieldcat
*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*Layout
FORM build_layout.
it_layout-zebra = 'X'.
it_layout-colwidth_optimize = 'X'.
ENDFORM. "build_layout
*&---------------------------------------------------------------------*
*& Form alv_display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*ALV output
FORM alv_display.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'PFSTATUS'
it_fieldcat = it_fieldcat
is_layout = it_layout
it_event_exit = it_eventexit
i_screen_start_column = 10
i_screen_start_line = 20
i_screen_end_column = 70
i_screen_end_line = 45
i_grid_title = 'Call Tcode Refresh ALV'
TABLES
t_outtab = it_tstc.
ENDFORM. "alv_display
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*User actions on ALV
FORM user_command USING r_ucomm TYPE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
*User clicks a transaction code and that tcode is called from ALV
WHEN '&IC1'.
READ TABLE it_tstc INDEX rs_selfield-tabindex INTO wa_tstc.
IF sy-subrc = 0.
CALL TRANSACTION wa_tstc-tcode.
ENDIF.
*user clicks the refresh button and the next 25 records are displayed
WHEN '&REFRESH'.
PERFORM fetch_data.
rs_selfield-refresh = 'X'.
rs_selfield-col_stable = 'X' .
rs_selfield-row_stable = 'X' .
ENDCASE.
ENDFORM. "user_command
*---------------------------------------------------------------------*
* FORM PFSTATUS *
*---------------------------------------------------------------------*
*Form for settings the pf status to the alv
FORM pfstatus USING ut_extab TYPE slis_t_extab.
SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'.
ENDFORM. " PF_STATUS_SET

You might also like