Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
AbapWB_content_EN.doc
Скачиваний:
1
Добавлен:
01.03.2025
Размер:
40.44 Mб
Скачать

When ’rw’.

CALL FUNCTION ’POPUP_TO_CONFIRM’ EXPORTING

titlebar = text-002

text_question = text-001 display_cancel_button = space

Importing

answer = answer. CASE answer.

WHEN ’1’.

SET SCREEN 0. WHEN ’2’.

SET SCREEN 100.

ENDCASE.

WHEN ’SAVE’.

MOVE-CORRESPONDING sdyn_book TO wa_sbook.

CALL FUNCTION ’BC400_UPDATE_BOOK’ EXPORTING

iv_book = wa_sbook

EXCEPTIONS

book_not_found = 1 update_sbook_rejected = 2 book_locked = 3 currency_conversion_error = 4

OTHERS = 5.

IF sy-subrc = 0.

MESSAGE s148(bc400). SET SCREEN 0.

ELSE.

MESSAGE e149(bc400). ENDIF.

ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

Lesson Summary

You should now be able to:

• Search for function modules

• Acquire information on the functionality and use of function modules

• Call a function module in your program

Lesson:

331

Working with Methods

Lesson Duration: 80 Minutes

Lesson Overview

SAP provides many functions in the SAP system as classes or methods. In this lesson, you will learn about some basic object-oriented syntax elements of ABAP objects using the ALV List Viewer as an example. You can use the List Viewer to display an internal table graphically formatted on a screen.

This lesson is not intended to provide you with in-depth knowledge on programming the ALV List Viewer, but rather to provide a brief introduction to the benefits of existing classes and methods.

Lesson Objectives

After completing this lesson, you will be able to:

• Execute the basic steps in object-oriented ABAP programming (instantiation and method calls) to use the classes and methods shipped in the SAP standard system.

• Use the SAP Grid Control (ALV List Viewer) to display an internal table on a screen

The didactic concept of this course — maximum use with minimum effort

— should also be emphasized in this lesson. Therefore, the subject of object orientation will only be discussed in as much detail as is required for a user of the predefined components.

At this point, it is certainly worth mentioning the follow-up course BC401, which provides a complete introduction to object orientation. Participants wanting to find out more about control programming should be referred to course BC412.

However, it should become clear that object-oriented programming is not mandatory in ABAP, but is used in some standard SAP applications.

Business Example

You want to use the standard functions in your program that are encapsulated in global classes.

Working with Methods

Figure 205: Classes and Objects

A class is a formal description of objects (instances). You can create several instances of a class at runtime, each of which will have its own attributes and methods. You can access the corresponding attributes of the affected instance by calling an instance method.

Let's take flight bookings as an easy-to-follow example. Each instance of this class corresponds to an actual booking and has its own booking-specific attributes such as booking ID, booking date, cancellation flag, and so on. A method of a booking instance could, for example, execute the cancellation of the booking.

The call of this method of an actual booking instance would then change the corresponding attribute of the booking, the “cancellation flag”.

Classes can be defined either locally within a program or globally in the class library.

SAP has shipped many global classes with methods that encapsulate the required functionality for reusability. Hence, these classes and methods also belong to

the reuse components.

Hint: SAP has extended the ABAP language with object-oriented syntax elements (ABAP Objects) since release 4.6. However, due to time restrictions, not all of them can be dealt with in this course. This

lesson will only illustrate the basic steps in order to enable you to use the classes and methods shipped by SAP. For more detailed information on object-oriented programming refer to course BC401.

Figure 206: Creating Objects and Calling Methods

As instances do not have names, you have to define reference variables in order to be able to generate and address instances of classes. They are pointers that can be directed to corresponding instances. Reference variables each have a name that can be used to address the corresponding instance.

Reference variables are defined using:

DATA reference_name TYPE REF TO class_name.

When the program is started, a reference variable still has its initial value (“does not point to an instance”). Once it has been used to create an instance, it no longer has the initial value points to that instance.

Selection options are defined when you use the

CREATE OBJECT reference_name.

created an instance of the class that was specified in the definition of the reference variables. Afterwards, the reference variable points to the newly created instance.

When you use CREATE OBJECT, you might have to supply the import parameters of the special method CONSTRUCTOR with data. This special method is automatically executed directly after the creation of the instance. With its import parameters, it supplies the corresponding attributes of the new instance with values.

You call methods of an instance using the statement

CALL METHOD reference_name->method_name.

In contrast to calling a function module, the method name alone does not suffice here. You have to specify the relevant instance as well, as it is possible that the program has several instances of that class.

Figure 207: Example of Standard Classes: Classes of EnjoySAP Controls

In addition to the control overview depicted in the above graphic, you should show the participants the menu path Environment → Examples → Control Examples

in the Object Navigator and refer to course BC412.

With release 4.6, SAP has shipped many EnjoySAP Controls, with which you can design screens more ergonomically and interestingly. The above graphic shows a selection of these controls:

• Grid Control : To display an internal table on a screen with many interesting functions such as sort, filter, total and others.

• Picture Control : To display a picture on the screen

• HTML-Viewer Control : To display an HTML file or Web page on the screen

• Tree Control : To depict a hierarchical list in the form of a tree structure on the screen

The controls are addressed by using the classes and methods that are shipped in the SAP standard system. In the following, we will use class CL_GUI_ALV_GRID as an example of the reuse of existing classes in order to address the most interesting and popular ALV Grid Control.

For detailed information on all EnjoySAP controls as well as interactions between them, refer to course BC412.

Figure 208: Application Example: ALV Grid Control

ALV Grid Control, also called SAP List Viewer (ALV) is used for displaying an internal table on a screen. It has numerous user functions.

On the screen, the user can vary the width of the columns, or the width can be automatically adjusted to the current data. The display position of the columns can also be changed by means of drag-and-drop.

The standard pushbuttons of the control can be used to execute, amongst others, the following functions:

The detailed display shows the fields which were previously selected with the cursor in a modal dialog window.

The sorting function provides the user with the option of specifying complex sorting criteria for the columns.

Within the selected area, you can use the search function for searching for a character string in rows or columns.

You can form totals for one or each of several numerical columns. You can then use the Subtotals function to set up control level lists. Select the non-numeric columns that you want to use before choosing this function and the corresponding control level totals are displayed.

You can also Print and Download with the corresponding pushbuttons.

The user can save his or her settings in the Grid Control as a display variant and reuse them at a later time.

Figure 209: Runtime Architecture of the SAP Grid Control

An EnjoySAP Control must always be embedded in a SAP Container Control (from now on referred to as container). The container must be integrated into a prepared control area on the screen. (See the left part of the above graphic)

In order to implement the Grid Control and the container from a GUI perspective, corresponding instances have to be created as substitutes within the program.

You can use these instances to address the elements in the GUI. To do so, your SAP system has standard classes from which you can generate the container and grid control instance.

Figure 210: Classes for Accessing Containers and Grid Controls

When you create an instance, the class-specific constructor (special method CONSTRUCTOR of the class) is called implicitly. The task of this method is to use its own input parameters to fill the attributes of the instance to be created. Hence, you must supply the required import parameters of the constructor with values when you create an instance (CREATE OBJECT).

Figure 211: The CONSTRUCTOR Method of the Container Class

To obtain detailed information on a global class or method, you can navigate to the Class Builder:

Display the object list of the class in the navigation area of the Object Navigator. Double-clicking on the class takes you to the detailed display in the Class Builder. (Alternatively, you can also go to the Class Builder by double-clicking the class names from within an ABAP program.) Select the required method with the cursor and press the Parameter pushbutton to display the interface parameters of the method.

Method CONSTRUCTOR of global class CL_GUI_CUSTOM_CONTAINER (class for the container) has the required parameter CONTAINER_NAME. Hence, as a minimum, this parameter has to be supplied with data when the container instance is created, namely with the name of the control area available on the screen.

Figure 212: Important Methods for the Grid Control Class

Global class CL_GUI_ALV_GRID has numerous methods that can be called for the corresponding grid control functions. To display the contents of an internal table with an ALV Grid Control, it suffices to know details about the following three methods:

CONSTRUCTOR

The grid class, too, has a constructor. The only required parameter is i_parent, to which the already created container instance (in the form of a pointer) has to be transferred (when the grid control instance is created).

SET_TABLE_FOR_FIRST_DISPLAY

This method of the created grid control instance is used for transferring data and settings to the grid control. The internal table to be displayed must be transferred to parameter IT_OUTTAB. This must be a standard table (see type definition of the parameter).

Furthermore, technical information is required for the output of the grid columns. The easiest thing to do is to use a Dictionary structure or transparent table as the row description of the internal table. In this case, all you have to do is transfer the name of the Dictionary object to the parameter I_STRUCTURE_NAME. (Alternatively, you can also set up a field catalog and transfer it to parameter IT_FIELDCATALOG.)

REFRESH_TABLE_DISPLAY

You only have to call this method if the table content, which has changed in the meantime, is to be loaded for updating the display of the grid control in case the screen is processed again.

340

Displaying an Internal Table in ALV Grid Control on a Screen

Figure 213: Creating a Screen Element "Custom Control Area"

1. In the Graphic Layout Editor, you can define a control area on your screen.

To do so, choose the pushbutton Custom Control from the toolbar. Now select it and specify the size and position of the area on the screen as follows:

Click the editing area where you want to place the top left corner of the custom control and hold down the mouse key. Drag the cursor diagonally down to the right to where you want the bottom right corner. Once you release the mouse button, the bottom right corner is fixed in position.

Enter a new name for the screen element (here: CONTAINER_1).

Use the Resizing Vertical and Resizing Horizontal attributes to specify whether or not the area of the custom control should be resized when the main screen is resized. When you set these attributes, you can also set the minimum values for the area using the additional attributes Min. Lines and Min. Columns.

Continued on next page

Figure 214: Defining Reference Variables

2. Two reference variables are required in the ABAP program:

• A reference variable that is to point to a container instance that is yet to be created (name here: CONTAINER_R)

• A reference variable that is to point to a grid control instance that is yet to be created (name here: GRID_R)

Figure 215: Creating Instances

Continued on next page

3. You create the instances with the CREATE OBJECT statement. You should definitely have your statement generated in your source code in order to avoid typing errors and omissions. To do so, display the object list of the respective class in the navigation area of the Object Navigator, drag and drop the class name in your source code.

In the generated call, you must now insert the name of the reference variable for xxxxxxxx and supply the parameters with values. The call syntax is very similar to that of the function module. However, the parameter to be supplied with values with CREATE OBJECT are the interface parameters of the respective constructor.

It is mandatory to implement the creation of the control instances before

displaying the screen. This is frequently done in a PBO module.

You only have to instantiate the controls on a screen once. This means that this step is omitted when a screen is processed again. This is easily done by querying one of the two reference variables:

IF container_r IS INITIAL.

Figure 216: Calling Methods

4. To transfer the content of an internal table and its row description to the SAP Grid Control, call method SET_TABLE_FOR_FIRST_DISPLAY of the grid control instance. Here, you should also have the call generated by

Continued on next page

means of drag-and-drop. In the generated call, you then have to use the name of the grid control reference variable instead of xxxxxxxx and supply

the parameters with values:

• You then transfer the filled internal table to parameter IT_OUTTAB.

• Here the internal table has been typed with the transparent table SPFLI as row type. Therefore, it is sufficient to pass this name to the I_STRUCTURE_NAME parameter. The corresponding Dictionary information is then loaded automatically and transferred to the control.

If the content of the internal table changes during the program, you must call the REFRESH_TABLE_DISPLAY method in order to update the grid display before the next screen is displayed.

345 Exercise 19: Using Methods

Exercise Duration: 40 Minutes

Exercise Objectives

After completing this exercise, you will be able to:

• Display simple lists using an ALV Grid Control

Business Example

Your previous lists are to be displayed in ALV Grid Control.

System Data

System: Will be assigned Client: Will be assigned User ID: Will be assigned Password: Will be assigned

Set up instructions: No special instructions when using a standard training system

Task 1:

Prepare data and screen for control display

1. Copy the program SAPBC400RUT_ALV_GRID including all of its components to the new name ZBC400_##_ALV_GRID.

2. Get to know the program.

In the template, there is an internal table that matches the database table

SPFLI and also a screen with the number 100.

3. Fill the internal table with data records from the data table SPFLI using an array fetch.

4. Create a container control area on the screen 100 (name proposal:

CONTAINER_1).

Task 2:

Create container and control, data transfer to the control

1. Define a reference variable for each of the classes CL_GUI_CUS- TOM_CONTAINER and CL_GUI_ALV_GRID.

2. Implement the control or container instance creation at the event PROCESS BEFORE OUTPUT of screen 100. Only supply the required parameters.

Continued on next page

3. At the point PROCESS BEFORE OUTPUT, call method SET_TABLE_FOR_FIRST_DISPLAY to transfer the data to the grid control. Supply the parameter I_STRUCTURE_NAME (row type of the internal table) with the name of a suitable Dictionary type and the parameter IT_OUTTAB with the actual internal table.

4. Ensure that the creation of the instances as well as the method call only happens when the screen is processed for the first time.

Solution 19: Using Methods

Task 1:

Prepare data and screen for control display

1. Copy the program SAPBC400RUT_ALV_GRID including all of its components to the new name ZBC400_##_ALV_GRID.

a) Carry out this step as usual.

2. Get to know the program.

In the template, there is an internal table that matches the database table

SPFLI and also a screen with the number 100. a) Carry out this step as usual.

3. Fill the internal table with data records from the data table SPFLI using an array fetch.

a) See source code excerpt in the model solution.

4. Create a container control area on the screen 100 (name proposal:

CONTAINER_1).

a) Carry out this step as described in the training material.

Task 2:

Create container and control, data transfer to the control

1. Define a reference variable for each of the classes CL_GUI_CUS- TOM_CONTAINER and CL_GUI_ALV_GRID.

a) See source code excerpt in the model solution.

2. Implement the control or container instance creation at the event PROCESS BEFORE OUTPUT of screen 100. Only supply the required parameters.

a) See source code excerpt in the model solution.

3. At the point PROCESS BEFORE OUTPUT, call method SET_TABLE_FOR_FIRST_DISPLAY to transfer the data to the grid control. Supply the parameter I_STRUCTURE_NAME (row type of the internal table) with the name of a suitable Dictionary type and the parameter IT_OUTTAB with the actual internal table.

a) See source code excerpt in the model solution.

4. Ensure that the creation of the instances as well as the method call only happens when the screen is processed for the first time.

a) See source code excerpt in the model solution.

Continued on next page

Result

Source code excerpt SAPBC400RUS_ALV_GRID

REPORT sapbc400rus_alv_grid .

...

DATA:

container_r TYPE REF TO CL_GUI_CUSTOM_CONTAINER, grid_r TYPE REF TO CL_GUI_ALV_GRID.

START-OF-SELECTION.

* fill internal table

SELECT * FROM spfli INTO TABLE gdt_spfli.

CALL SCREEN 100.

Flow logic for screen 100:

PROCESS BEFORE OUTPUT.

MODULE status_0100. MODULE clear_ok_code.

MODULE create_control.

*

PROCESS AFTER INPUT.

MODULE user_command_0100.

Source code excerpt: Module CREATE_CONTROL

MODULE create_control OUTPUT.