Consuming CDS Views Using ALV with IDA in ABAP
ABAP List Viewer (ALV) is an efficient tool to display
tabular data in SAP applications. With the ALV with Integrated Data Access
(IDA), the data fetching and display processes are more optimized and
efficient, especially when working with large datasets.
What is ALV with
IDA?
ALV with IDA is a technology that allows you to work with
very large datasets in an optimized manner. When combined with Core Data
Services (CDS) views, it allows you to fetch data directly from the database,
benefiting from CDS's optimized query capabilities.
The main advantage of ALV with IDA is that it pushes the
sorting, filtering, and aggregation operations to the database level, reducing
the load on the application server and improving performance, especially when
dealing with large data volumes.
Steps
to Consume CDS View Using ALV with IDA
Let’s go through the steps involved in consuming a CDS
view and displaying the data using ALV with IDA. We will also explain each part
of the code for better understanding.
1. Get the
Reference of ALV Object
The first step is to create a reference to the ALV object
using the CL_SALV_GUI_TABLE_IDA class. This class is responsible for binding
the CDS view to the ALV for data retrieval and display.
abap
CREATE method>CL_SALV_GUI_TABLE_IDA.
In the code below, we call the create_for_cds_view method
of the CL_SALV_GUI_TABLE_IDA class. This method binds the CDS view to the ALV
for further processing.
Code Example:
abap
CALL METHOD cl_salv_gui_table_ida=>create_for_cds_view
EXPORTING
iv_cds_view_name =
'zcds_view3' "CDS VIEW NAME
RECEIVING
ro_alv_gui_table_ida = ob1.
iv_cds_view_name:
This is the name of the CDS view you want to consume. Replace 'zcds_view3' with
the name of your CDS view.
ro_alv_gui_table_ida:
This parameter returns the reference to the ALV object.
At this point, you have successfully created an ALV
object that is linked to your CDS view.
2. Get the
Reference of ALV Full Screen Mode
Once the ALV object is created, we need to switch it to full
screen mode so the data can be displayed in a full screen layout. The method full
screen from the ALV object allows you to do this.
abap
FULL SCREEN method>IF_SALV_GUI_TABLE_IDA.
In the code below, we use the full screen method to
enable full screen display and store the reference to the fullscreen object in ob2.
Code Example:
abap
IF ob1 IS BOUND.
CALL METHOD ob1>fullscreen
RECEIVING
ro_fullscreen
= ob2.
ENDIF.
ob1 IS BOUND: We
check if the ALV object ob1 is successfully created and bound before proceeding
to the full screen mode.
ro_fullscreen:
This parameter stores the reference to the full screen object.
3. Display the
Data on the UI
Finally, after setting the ALV object to full screen
mode, we call the display method to show the data on the UI. This method brings
the tabular data from the CDS view and presents it using the ALV interface.
abap
DISPLAY method > IF_SALV_GUI_FULLSCREEN_IDA.
In the code below, the display method is used to render
the data on the UI.
Code Example:
abap
IF ob2 IS BOUND.
ob2>display(
).
ENDIF.
ob2 IS BOUND: We
check if the fullscreen object is successfully created before calling the
display method.
ob2>display( ):
This method displays the data fetched from the CDS view on the UI using ALV.
Full
Program Example
Here’s the complete program for consuming a CDS view and
displaying it using ALV with IDA.
abap
REPORT zcds_program_name.
DATA : ob1 TYPE REF
TO if_salv_gui_table_ida,
ob2 TYPE REF TO
if_salv_gui_fullscreen_ida.
CALL METHOD
cl_salv_gui_table_ida=>create_for_cds_view
EXPORTING
iv_cds_view_name = 'zcds_view_name' "CDS VIEW NAME
RECEIVING
ro_alv_gui_table_ida = ob1.
IF ob1 IS BOUND.
CALL METHOD ob1>fullscreen
RECEIVING
ro_fullscreen = ob2.
ENDIF.
IF ob2 IS BOUND.
ob2>display( ).
ENDIF.
Key Points to Remember
1. CL_SALV_GUI_TABLE_IDA: This is the core class used to
consume a CDS view and display it using ALV with IDA.
2. ALV Object: The create_for_cds_view method binds the
CDS view to the ALV object.
3. FullScreen Mode: The fullscreen method allows you to
display data in fullscreen mode.
4. Display Method: The display method renders the data on
the UI.
5. Efficiency: ALV with IDA optimizes performance by
pushing database operations like sorting and filtering to the database layer.
Using ALV with IDA to consume CDS views in ABAP programs
is a highly efficient way to handle large datasets. The process is streamlined
and enhances performance by pushing dataheavy operations to the database. With
the steps above, you can create a clean and efficient interface for displaying
data directly from CDS views.
This approach is particularly beneficial in S/4HANA systems where you can leverage the inmemory capabilities of HANA combined with CDS views' optimization features.
Code with exception class:
REPORT ZDCONSUME_PROGRAM_NAME.
DATA : O_EX1 TYPE REF TO CX_SALV_DB_CONNECTION,
O_EX2 TYPE REF TO CX_SALV_DB_TABLE_NOT_SUPPORTED,
O_EX3 TYPE REF TO CX_SALV_IDA_CONTRACT_VIOLATION,
O_EX4 TYPE REF TO cx_salv_param_out_of_bounds,
o_ex5 type ref to cx_salv_function_not_supported,
OB1 TYPE REF TO IF_SALV_GUI_TABLE_IDA,
OB2 TYPE REF TO IF_SALV_GUI_FULLSCREEN_IDA,
V_MSG TYPE STRING.
TRY.
CALL METHOD cl_salv_gui_table_ida=>create_for_cds_view
EXPORTING
iv_cds_view_name = 'zcdsviewname'
receiving
ro_alv_gui_table_ida = ob1.
CATCH cx_salv_db_connection INTO O_EX1.
CLEAR V_MSG.
CALL METHOD o_ex1->if_message~get_text
RECEIVING
result = V_MSG.
WRITE :/ 'SHORT EXCEPTION MSG IS :', V_MSG.
CLEAR V_MSG.
CALL METHOD o_ex1->if_message~get_longtext
RECEIVING
result = V_MSG.
WRITE :/ 'LONG EXCEPTION MSG IS :', V_MSG.
CATCH cx_salv_db_table_not_supported INTO
O_EX2.
CLEAR V_MSG.
CALL METHOD o_ex2->if_message~get_text
RECEIVING
result = V_MSG.
WRITE :/ 'SHORT EXCEPTION MSG IS :', V_MSG.
CLEAR V_MSG.
CALL METHOD o_ex2->if_message~get_longtext
RECEIVING
result = V_MSG.
WRITE :/ 'LONG EXCEPTION MSG IS :', V_MSG.
CATCH cx_salv_ida_contract_violation INTO
O_EX3.
CLEAR V_MSG.
CALL METHOD o_ex3->if_message~get_text
RECEIVING
result = V_MSG.
WRITE :/ 'SHORT EXCEPTION MSG IS :', V_MSG.
CLEAR V_MSG.
CALL METHOD o_ex3->if_message~get_longtext
RECEIVING
result = V_MSG.
WRITE :/ 'LONG EXCEPTION MSG IS :', V_MSG.
CATCH cx_salv_function_not_supported INTO
O_EX5.
CLEAR V_MSG.
CALL METHOD o_ex5->if_message~get_text
RECEIVING
result = V_MSG.
WRITE :/ 'SHORT EXCEPTION MSG IS :', V_MSG.
CLEAR V_MSG.
CALL METHOD o_ex5->if_message~get_longtext
RECEIVING
result = V_MSG.
WRITE :/ 'LONG EXCEPTION MSG IS :', V_MSG.
ENDTRY.
IF OB1 IS BOUND.
TRY.
CALL METHOD ob1->fullscreen
RECEIVING
ro_fullscreen = OB2.
CATCH cx_salv_ida_contract_violation INTO
O_EX3.
CLEAR V_MSG.
CALL METHOD o_ex3->if_message~get_text
RECEIVING
result = V_MSG.
WRITE :/ 'SHORT EXCEPTION MSG IS :', V_MSG.
CLEAR V_MSG.
CALL METHOD o_ex3->if_message~get_longtext
RECEIVING
result = V_MSG.
WRITE :/ 'LONG EXCEPTION MSG IS :', V_MSG.
ENDTRY.
ENDIF.
IF OB2 IS BOUND.
OB2->DISPLAY( ).
ENDIF.