You will learn
- How to create a global class that retrieves data from the back end
- How to use an internal table
- How to use an ABAP structure as a type for the returning parameter
- How to display data in a SAP List Viewer (“
ALV Grid
”)
Step 1: Open your ABAP program and remove the WRITE statement
-
First, open your ABAP program,
ZSO_INVOICE_ITEMS_EURO
which you created in the previous tutorial, Create and run an ABAP application. -
Remove the existing method implementation for the
run
method.
Step 2: Create an instance of a new global class
Now you will create the an object with the type of a new global class, for retrieving backend data.
- In the
run
method, create an instance of classzcl_invoice_retrieval
using thenew
operator:
ABAP
Copy
data(invoices) = new ZCL_INVOICE_RETRIEVAL( ).
-
Since this class does not yet exist, you will get a syntax error. To create the class, place the cursor on the class name and open the Quick Assist by choosing Ctrl+1. In the Quick Assist menu, double-click on Create global class
zcl_invoice_retrieval
: -
A wizard will appear to create a new ABAP class. Enter:
- a name
ZCL_INVOICE_RETRIEVAL
- a description invoice Retrieval
- a name
-
Choose Finish:
A new editor will be opened showing the class you have created, ZCL_INVOICE_RETRIEVAL
.
Since release 7.40, ABAP permits inline declarations. For more information, see: - Old and new ABAP syntax – overview sheet - 7.4 Release News - Inline Declaration I
Step 3: Check the syntax
If necessary, go back to your program and trigger the syntax check using the keyboard shortcut Ctrl+F2
.
The syntax error should no longer occur.
Step 4: Create a method to get the database records
To read the records from the database, you need to call a method get_items_from_db
.This method does not yet exist so we will create it with a Quick Assist as follows:
-
Still in your program, enter an instance method call:
ABAP
Copy
data(invoice_items) = invoices->get_items_from_db( ).