You are on page 1of 4

8/27/2018 Object Model ALV - Interactive - ABAP Development - SCN Wiki

Getting Started Store

Community WIKI SAP Community Welcome, Guest Login Register Search the Community

ABAP Development

Object Model ALV - Interactive


Created by Former Member, last modified by Bret Halford on Jul 11, 2013

Interactive Object Model ALV

Object Model ALV surprises with its fast and efficient way of generating reports. We will go step by step and learn how easily we can create Interactive ALV report.

There are three main classes (Fig 1) supported by Object Model ALV to generate

1) Two dimensional table

2) Hierarchical Sequential List

3) Tree Structure

Unknown macro: {gliffy}

(Fig 1.)

Starting with an example of creating a two dimensional table display in full screen display type.

I have taken a very simple example of displaying sales document item level data, which takes document number as an input.

Reference Table: VBAP - Sales Document Item.

Firstly we have to design a selection screen, since we are going a little simple, I have just created a selection option on document number i.e. VBAP-VBELN.

*------------------*
* Selection Screen
*------------------*
SELECT-OPTIONS: s_vbeln FOR vbap-vbeln.

Data Declaration: Now that we have created our little selection screen, we will do the data declaration as follows -

We have to create an instance of class CL_SALV_TABLE.

*-------------------*
* Data Declaration
*-------------------*

DATA: g_alv TYPE REF TO cl_salv_table,


g_alv_functions TYPE REF TO cl_salv_functions,
g_display TYPE REF TO cl_salv_display_settings,
g_agg TYPE REF TO cl_salv_aggregations.

TYPES: BEGIN OF t_vbap,


vbeln TYPE vbap-vbeln,
matnr TYPE vbap-matnr,
netwr TYPE vbap-netwr,
END OF t_vbap.

DATA: i_vbap TYPE STANDARD TABLE OF t_vbap, "Internal Table


w_vbap LIKE LINE OF i_vbap. "Work Area

Start of Selection screen: Now we will use start of selection event to fill out internal table i_vbap.

SELECT vbeln matnr netwr


FROM vbap
INTO TABLE i_vbap
WHERE vbeln IN s_vbeln.

On successful retrieval of values in the internal table, we will have to call the factory method of class CL_SALV_TABLE. We can open the same in SE80 Patterns.

TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = g_alv
CHANGING
t_table = i_vbap.
CATCH cx_salv_msg.
ENDTRY.

Would you believe if I say ALV list is complete after you call display method of CL_SALV_TABLE class?

g_alv->display( ).

Yes , ALV list is ready, isn't it fast? Let's learn how to make it interactive. We will create a button on application tool bar. To create a button, we will have to set the GUI Status.

In SE80, function group, and enter SALV_TABLE_STANDARD and copy it to the report. After PF status has been copied, we set screen status

*------------------------*
* Making ALV Interactive.
*------------------------*
g_alv->set_screen_status (pfstatus = 'SALV_TABLE_STANDARD'

https://wiki.scn.sap.com/wiki/display/ABAP/Object+Model+ALV+-+Interactive 1/4
8/27/2018 Object Model ALV - Interactive - ABAP Development - SCN Wiki
report = sy-repid
set_functions = g_alv->c_functions_all ).

Here c_functions_all, sets all function keys. Similarly there are other constants like C_FUNCTIONS_NONE and C_FUNCTIONS_DEFAULT. ALV object model has four selection modes.

Selection Mode Constants Single


rows SINGLE Multiple Rows
MULTIPLE Rows and Columns
ROW_COLUMN Particular Cell CELL

g_selections = g_alv->get_selections( ).
g_selections->set_selection_mode( g_selections->single ).

After we have set the selection mode, we create an event handler class. When the event is triggered method on_user_command is called and OK_CODE is captured in e_salv_function, based on which
case statement executes.

*&----------------------------------------------*
*& Class LCL_EVENT_HANDLER
*&----------------------------------------------*
CLASS lcl_handle_events DEFINITION.
PUBLIC SECTION.
METHODS: on_user_command FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function.
ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

*-------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*--------------------------------------------------------*
CLASS lcl_handle_events IMPLEMENTATION.
METHOD on_user_command.
CASE e_salv_function.
WHEN 'MYFUNCTION'.
g_selections = g_alv->get_selections( ).
MESSAGE : i001(00) WITH 'You have selected a row '.
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "on_user_command
ENDCLASS. "lcl_event_handler IMPLEMENTATION

Please see the code below, in which I have used extra classes like - cl_salv_functions, cl_salv_display_settings,
cl_salv_aggregations.

Code Example

TABLES: vbap.

*------------------*
* Selection Screen
*------------------*
SELECT-OPTIONS: s_vbeln FOR vbap-vbeln.

*-------------------*
* Data Declaration
*-------------------*

DATA: g_alv TYPE REF TO cl_salv_table,


g_alv_functions TYPE REF TO cl_salv_functions,
g_display TYPE REF TO cl_salv_display_settings,
g_agg TYPE REF TO cl_salv_aggregations,
g_selections TYPE REF TO cl_salv_selections,
g_events TYPE REF TO cl_salv_events_table,
g_rows TYPE salv_t_row.

TYPES: BEGIN OF t_vbap,


vbeln TYPE vbap-vbeln,
matnr TYPE vbap-matnr,
netwr TYPE vbap-netwr,
END OF t_vbap.

* Internal Table
DATA: i_vbap TYPE STANDARD TABLE OF t_vbap.
* Work area
DATA: w_vbap LIKE LINE OF i_vbap.

*&-------------------------------------------------*
*& Class LCL_EVENT_HANDLER
*&-------------------------------------------------*
CLASS lcl_handle_events DEFINITION.
PUBLIC SECTION.
METHODS: on_user_command FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function.
ENDCLASS. "LCL_EVENT_HANDLER DEFINITION

DATA : g_event_handler TYPE REF TO lcl_handle_events.

*---------------------*
* Start of Selection
*---------------------*
START-OF-SELECTION.
SELECT vbeln matnr netwr
FROM vbap

https://wiki.scn.sap.com/wiki/display/ABAP/Object+Model+ALV+-+Interactive 2/4
8/27/2018 Object Model ALV - Interactive - ABAP Development - SCN Wiki
INTO TABLE i_vbap
WHERE vbeln IN s_vbeln.
IF sy-subrc = 0.
*----------------------------------*
* Create the instance of the class.
*----------------------------------*
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = g_alv
CHANGING
t_table = i_vbap.
CATCH cx_salv_msg .
ENDTRY.
* ALV functions
g_alv_functions = g_alv->get_functions( ).
g_alv_functions->set_all( abap_true ).
**---------------------*
** Display Settings
**---------------------*
g_display = g_alv->get_display_settings( ).
g_display->set_striped_pattern( cl_salv_display_settings=>true ).
g_display->set_list_header('Heading by Ashish Rawal').
ENDIF.

*-------------------------*
* Aggregation Functions
*-------------------------*
g_agg = g_alv->get_aggregations( ).
g_agg->add_aggregation('NETWR').

*------------------------*
* Making ALV Interactive.
*------------------------*
g_alv->set_screen_status( pfstatus = 'SALV_TABLE_STANDARD'
report = sy-repid
set_functions = g_alv->c_functions_all ).

g_selections = g_alv->get_selections( ).
g_selections->set_selection_mode( g_selections->single ).

*--------------------------*
* Event Handling Starts
*--------------------------*
g_events = g_alv->get_event( ).
CREATE OBJECT g_event_handler.
SET HANDLER
g_event_handler->on_user_command FOR g_events.

*-----------------------*
* Displaying the ALV
*-----------------------*
g_alv->display( ).

*-------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*-------------------------------------------------------------------*
CLASS lcl_handle_events IMPLEMENTATION.
METHOD on_user_command.
CASE e_salv_function.
WHEN 'MYFUNCTION'.
g_selections = g_alv->get_selections( ).
MESSAGE : i001(00) WITH 'You have selected a row'.
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "on_user_command
ENDCLASS. "lcl_event_handler IMPLEMENTATION
code gallery netweaver sdn teched sap abap object tips wiki

7 Comments
Guest
Hi,

Its a very good way of doing ALV using Object concept. It actually helps in reducing the code & the time for the developer for the deliverables. So thanks you for suggesting a new way.

Thanks,

Debi.

Former Member
Hi Ashish, This is a good way of getting rid of old conventional ALV. This makes it fast and easy to generate report.

Expect more on Tree and Hierarchical.

Gopi.

https://wiki.scn.sap.com/wiki/display/ABAP/Object+Model+ALV+-+Interactive 3/4
8/27/2018 Object Model ALV - Interactive - ABAP Development - SCN Wiki

Guest

This is an extremely useful post that you have put together. It is one of its kind and i wish that you would continue to post similar stuff for all of us to learn.

With the experience of people like yourself, we can learn a lot.

Your's is one of the coolest blogs i have come across. Thanks a lot!!!!

Priyanka

Guest
Hey Ashish,

Good job. I liked this sample pretty much and would advocate this to be used in my projects. It reduces the effort by huge number and I only concentrate on the effort taken to do the job
correctly meeting the client requirements.

Good job..

Abhinav

Former Member
Hi Ashish,

Its too good.

I liked it and got the easy way to build an ALV, instead of writing long code we can create an ALV using few lines of code with so many options and minimal time.

Now it became easy for me to develop an ALV in less time.

Thanks Ashish...

Shweta.

Former Member
Hi,

Great job.
I think the function group name is missing. I guess this should be 'SALV_METADATA_STATUS'.

Best regards,
Guillaume

Guest
Hi Ashish,

I am new to OO ABAP. I tried your code but still i got run-time error which is

CX_SALV_OBJECT_NOT_FOUND. Please let me know what problem.

Contact Us SAP Help Portal


Privacy Terms of Use Legal Disclosure Copyright Cookie Preferences Follow SCN

https://wiki.scn.sap.com/wiki/display/ABAP/Object+Model+ALV+-+Interactive 4/4

You might also like