Step-by-Step Guide to Creating Object-Oriented ALV in ABAP
In this blog post, we will explore how to create an Object-Oriented ALV report in ABAP from scratch. We will walk through the process of preparing data, creating the ALV container, setting up the field catalog, and displaying the ALV in an SAP custom screen.
With Object-Oriented ALV, you can build dynamic, interactive reports that allow users to sort, filter, and export data with ease. Let's dive into the steps required to build this ALV report.
Steps to Create Object-Oriented ALV in ABAP
1.
Define the ALV Program
The first step in creating the ALV report is to define a simple program. Here, we will create an internal table to hold the data that will be displayed in the ALV grid. We also need to declare the ALV and container objects that will manage the display.
2.
Create the ALV Display Module
Once the internal table is defined, we need to set up the
ALV grid. This includes creating:
- A custom container for embedding the ALV on the screen.
- The ALV grid object that handles the display of the
data.
- A Field Catalog that controls how each column of the internal table will appear in the ALV.
The Field Catalog is used to assign descriptions to each column, which makes the ALV more user-friendly. We will create a form that sets up the field catalog for the columns "Employee ID", "Employee Name", and "Salary".
3.
Create the ALV Container in SE51
To display the ALV grid, we need to embed it inside a
custom container. This is done by creating a screen in SE51. Here's how:
1. Go to SE51: In the command field, type SE51 and press
Enter.
2. Create a Screen: Enter your program name and screen
number (100).
3. Set Screen Attributes: Choose "Normal
Screen" and ensure that the "Next Screen" is set to 0.
4. Layout Design:
- Add a Custom
Control to the screen layout. This control will serve as the container for the
ALV grid.
- Name the
custom control as ALV_CONTAINER, which must match the container name in the
code.
5. Save and Activate: Activate the screen and layout.
4.
Handle Screen Events (PBO and PAI)
We need to manage two key screen events:
1. PBO (Process Before Output): This event is triggered
before the screen is displayed, where the custom container and ALV grid are
created. It also sets the status and title for the screen.
2. PAI (Process After Input): This event is triggered when the user interacts with the screen, such as clicking buttons. In this case, it handles the "Back" button functionality.
5.
Final Program Flow
Once the screen and events are handled, the ALV will
display the data in the internal table in the custom container when the program
is executed. At runtime, the report will:
- Create a container for the ALV grid.
- Prepare data to be displayed.
- Set up the field catalog for the internal table.
- Display the ALV with the prepared data.
ABAP
Code
Here’s the full ABAP code for the Object-Oriented ALV report. Feel free to copy and modify it as per your project requirements.
abap
REPORT z_sampleprogram1.
* Declare internal table and structure
TYPES: BEGIN OF ty_employee,
emp_id TYPE
char5,
name TYPE char20,
salary TYPE p
DECIMALS 2,
END OF ty_employee.
DATA: gt_employee TYPE TABLE OF ty_employee,
gs_employee TYPE ty_employee.
* Declare ALV and container objects
DATA: go_alv_grid TYPE
REF TO cl_gui_alv_grid,
go_container TYPE
REF TO cl_gui_custom_container,
lt_fieldcat TYPE lvc_t_fcat,
ls_fieldcat TYPE lvc_s_fcat.
* Start-of-selection event to prepare data
START-OF-SELECTION.
* Call screen to display ALV
CALL SCREEN 100.
MODULE status_0100 OUTPUT.
SET PF-STATUS 'BACK'.
CREATE OBJECT
go_container
EXPORTING
container_name = 'ALV_CONTAINER'.
CREATE OBJECT
go_alv_grid
EXPORTING
i_parent =
go_container.
PERFORM prepare_data.
PERFORM fieldcat.
PERFORM display_alv.
ENDMODULE.
MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
FORM prepare_data.
CLEAR gs_employee.
gs_employee-emp_id =
'E001'.
gs_employee-name = 'John
Doe'.
gs_employee-salary =
'5000.00'.
APPEND gs_employee TO gt_employee.
CLEAR gs_employee.
gs_employee-emp_id =
'E002'.
gs_employee-name = 'Jane
Smith'.
gs_employee-salary =
'6000.00'.
APPEND gs_employee TO gt_employee.
CLEAR gs_employee.
gs_employee-emp_id =
'E003'.
gs_employee-name =
'Robert Brown'.
gs_employee-salary =
'5500.00'.
APPEND gs_employee TO
gt_employee.
ENDFORM.
FORM fieldcat .
"Set field catalog
ls_fieldcat-fieldname =
'EMP_ID'.
ls_fieldcat-seltext =
'Employee ID'.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname =
'NAME'.
ls_fieldcat-seltext =
'Employee Name'.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname =
'SALARY'.
ls_fieldcat-seltext =
'Salary'.
APPEND ls_fieldcat TO
lt_fieldcat.
ENDFORM.
FORM display_alv .
"Display ALV
CALL METHOD
go_alv_grid->set_table_for_first_display
EXPORTING
i_structure_name = '
' " No Dictionary structure, so
blank
CHANGING
it_outtab = gt_employee
it_fieldcatalog = lt_fieldcat.
ENDFORM.
Creating an Object-Oriented ALV in ABAP is a powerful way to display interactive reports with advanced features such as sorting, filtering, and exporting. By following this step-by-step guide, you can set up your own ALV report, customize the field catalog, and display data dynamically on the screen.