The following tutorial explains how to create a simple ALV report using class.
There is a standard class available in the repository named CL_GUI_ALV_GRID. Using the methods of this class we are now going to display the ALV report.
Scenario: Display the list of flight details for the given date range.
Requisites: Knowledge on object orientation on method calls and SQL statements to fetch the data from the database.
Procedure:
1. Get the details from the table SFLIGHT for the selected date range.
2. Build the layout structure to be displayed using field catalog.
3. Build an ALV Container and ALV Grid
4. Display the data in ALV format using SET_TABLE_FOR_FIRST_DISPLAY method of the class CL_GUI_ALV_GRID.
Step 1: Go to the Tcode SE38 (ABAP Editor) and create a report program. (Executable) Assign the program to the package or save it in the local object.
Enter the title for the report and Choose the program type as executable and click on save.
Step 2: Declare a select option to get the input date range from the user and Internal table and work area of type SFLIGHT as shown below to get the data from the database table.
REPORT ZCSK_ALV_SFLIGHT.
data : wa_sflight type sflight,
it_sflight type standard table of sflight.
select-options : s_fldate for wa_sflight-FLDATE.
Initialize the start of selection event and write the logic to fetch the data from the database table SFLIGHT for the given date range.
start-of-selection.
select * from sflight
into table it_sflight
where fldate in s_fldate.
if sy-subrc = 0.
sort it_sflight by carrid connid fldate.
endif.
Step 3: Build the layout structure of the data to be displayed using the field catalog. Create an internal table and work area of the type for fieldcatalog and Build layout for the columns to be displayed as shown below.
perform build_fcat using '1' 'CARRID' 'Carrier ID' '3'.
perform build_fcat using '2' 'CONNID' 'Conn. No' '10'.
perform build_fcat using '3' 'PRICE' 'Price' '10'.
perform build_fcat using '4' 'SEATSMAX' 'Seats Max' '10'.
perform build_fcat using '5' 'SEATSOCC' 'Seats Occ.' '10'.
*&---------------------------------------------------------------------*
*& Form BUILD_FCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form build_fcat using col_pos
fieldname
seltext
outputlen.
wa_fieldcat-col_pos = col_pos.
wa_fieldcat-fieldname = fieldname.
wa_fieldcat-seltext = seltext.
wa_fieldcat-outputlen = outputlen.
append wa_fieldcat to it_fieldcat.
clear wa_fieldcat.
endform. " BUILD_FCAT
Step 4: Create an Object reference for the ALV container and ALV Grid.
data : lo_container type ref to cl_gui_custom_container,
lo_alvgrid type ref to cl_gui_alv_grid.
create object lo_container
exporting
container_name = 'ALV_CONT' “This is the name to be given to the custom container in the screen.
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
others = 6.
if sy-subrc = 0.
create object lo_alvgrid
exporting
i_parent = lo_container
exceptions
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
others = 5.
if sy-subrc = 0.
* Call the method to display the data.
call method lo_alvgrid->set_table_for_first_display
changing
it_outtab = it_sflight
it_fieldcatalog = it_fieldcat
exceptions
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
others = 4.
* In the layout of the screen create a custom container with the name 'ALV_CONT’
call screen 9001.
endif.
Output:
Hello, you are missing the data declarations of It_fieldcat and wa_fieldcat. Could be confusing.
ReplyDeletenice
ReplyDeletecan you tell me how to use in class and method
You can add this data declarations for the it_fieldcat and wa_fieldcat.
ReplyDeleteDATA: it_fieldcat TYPE lvc_t_fcat,
wa_fieldcat TYPE lvc_s_fcat.
And it functions correctly.
I get an error, that Dynpro 9001 does not exist.
ReplyDeleteDo you know how i can fix that?