Anthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 17 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at [email protected].
Problems with ABAP output and why using ALV can help
SALV – The Common Model
To avoid this SAP has developed common model – which is entirely based on object oriented design and coding. This new ALV object oriented model has many advantages:
Unified Object models: This model has only one main class which will get and set the parameters of entire layout.
Main Classes:
These are the main classes for the different structures of ALV:
All Classes use a static method FACTORY which will get back the instance of the ALV. This is an ABAP Object Oriented Programming Design Pattern. Specifically it is the Factory-Method Pattern. Why use this? At a very high-level, it hides all the complexity of the instantiating an object from the consumer.
For the simple 2D table display we must call the method CL_SALV_TABLE=>FACTORY. This will get the instance of the ALV Object. This is known as a Design pattern. Specifically this is the Factory-Method design pattern. For more information on Object Design patterns, see the link below, but at a high level, using the Factory method design pattern hides all the complexity of the instantiating an object from the consumer.
ABAP Objects Design Patterns – Factory Method
In this post we will see how we can create a very simple table ALV output just by calling two methods. We will use the delivered IDES SPFLI Flight table. Each successive post will build on the previous so be sure to check back each month. Lets get started!
The BASE Code
You can cut and paste this into your development system and Activate it. It should work as-is.
REPORT zalvom_demo1. ************************************************************************ ** Global References for Classes ** ************************************************************************ DATA: gr_table TYPE REF TO cl_salv_table. ************************************************************************ ** Data Declarations ** ************************************************************************ DATA: it_spfli TYPE TABLE OF spfli. ************************************************************************ ** Processing Blocks ** ************************************************************************ START-OF-SELECTION. SELECT * INTO TABLE it_spfli FROM spfli. cl_salv_table=>factory( IMPORTING r_salv_table = gr_table CHANGING t_table = it_spfli ). gr_table->display( ).
OK, lets take each section of the code above and discuss why we are doing what we are doing….
Let’s start with getting some data to display. We use a simple SELECT Statement to retrieve the entire contents of table SPFLI into an Internal Table we have defined and TYPED to the SPFLI table.
************************************************************************ ** Data Declarations ** ************************************************************************ DATA: it_spfli TYPE TABLE OF spfli.
************************************************************************ ** Processing Blocks ** ************************************************************************ START-OF-SELECTION. SELECT * INTO TABLE it_spfli FROM spfli.
Next we need to create the ALV object for the 2D table. The FACTORY method allows you to create the ALV object in several ways. You can create the ALV Grid, as a classical list display, as a full screen grid, and finally embedded into a screen container. For our example, we will be working with the full screen grid. We create the call to the FACTORY method. We are importing the object reference into GR_TABLE and passing the internal table IT_SPFLI that has all the rows from the SELECT we did above.
************************************************************************ ** Global References for Classes ** ************************************************************************ DATA: gr_table TYPE REF TO cl_salv_table. cl_salv_table=>factory( IMPORTING r_salv_table = gr_table CHANGING t_table = it_spfli ).
Now what about actually displaying the results? Couldn’t be easier. We use the DISPLAY method from our instantiated ALV Object that was created for us via the Factory-Method Pattern. . We simply call it.
gr_table->display( ).
That’s it! You can quickly display any table this way. Depending on what rows you have in your SPFLI Table, your output should look something like this.
In Summary
We learned how to quickly create an ALV output for an SAP table using two (count’em just 2 Method calls)! All the OO is built into the Class for us. We used a Static call (=> vs. ->) to the Factory method of class cl_salv_table. This method instantiated the ALV object for us which allowed us to display the results using another method call to the display method.