Using The SALV OO Class for Quick and easy Table displays

97be63abb66b42f281dff9197b6b35da_WglrP9Anthony 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 ajcecchini@itpsap.com.

Problems with ABAP output and why using ALV can help

In this Blog series I will dive deeply into the SALV ABAP OO Class and elaborate on its advantages in ABAP over list processing. Lets begin by looking at some problems with “Classical” list processing…

 

Within an SAP system there is a lot of data that needs to be reviewed, maintained, and interpreted. You know what I’m talking about — inventory data, controlling data, accounting data, employee addresses, and so on and so forth. The bigger a company, the greater the amount of data that needs to be output for review and analysisWithin the context of certain applications, the terms “output” and “list” can be synonymous.

 

When I took my very first ABAP class at SAP Education, I learned that the term list denotes an output page that has been created using ABAP statements like WRITE, POSITION, or FORMAT, and text lines are buffered in a list buffer before a system program called the ABAP list processor steps in to perform various run-time activities. Lists created in this way are often referred to as classical lists (everything in SAP that is old is lovingly termed Classical) and represent the standard output method of data.

 

If you have dealt with list formatting issues and/or the use of events for interactive lists, you will find the features and functions introduced by an ALV report will make your life a lot simpler. Granted, the effort to create a classical list for just simple output of data is minimal. But amass a few lists across a few applications, each created by a different developer, and things get a bit more complicated.

 

The ABAP list  processor lacks standard baseline list handling functions like sorting, filtering, and calculations, so these types of functions have to be added by hand for each new list. Responsibility for list layout and interface design is also left to the developer. This is why you find different schemes for list handling and layout in different applications.

 

Lack of standard, baseline list processing functions means that developers devise their own methods for common list handling activities such as headings, sorting, filtering, rendering subtotals, and the like. The result is that end users who work with more than one application may have to deal with different ways to access these simple list functions based on the developer who coded the solution.

 

OK, Now you feel confident that using and ALV report or control is the way to go.

 

Now why use SALV?  Why not use the SAP ALV Grid Example I refer to in my previous Blog Series A Guide to the New ALV Grid Control ?

 

Prior to SAP Netweaver, we had so many different starting points to create the ALV. The starting point was entirely dependent on structure of the ALV (Tabular, Tree, Hierarchical), Type of ALV (List or Grid) etc…  If we wanted to use the Control framework, than we need to start the ALV by using the class CL_GUI_ALV_GRID as I described in my previous blog A Guide to the New ALV Grid Control. Moreover, we needed to make different calls for TOP-OF-PAGE, END-OF-PAGE etc… What’s the answer?

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:

Simplified design: This Model uses a highly integrated object oriented design which provides the simplicity to programmers to develop the ALV.

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:

SALV Classes

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.

SALV DEMO01 Output

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.

ITP logo

If you enjoyed this blog, Using The SALV OO Class for Quick and easy Table displays, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Comments (1)

[…] so in the last post – Using The SALV OO Class for Quick and easy Table displays, I gave you the base code for a simple 2D display using the ABAP OO SALV class. We will be adding […]

Comments are closed.

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!