Wednesday, June 10, 2009

Processing Input/Output Fields

Input/output fields can be either conventional fields in which the user can enter values using the keyboard or by selecting from a value list, or checkboxes or radio buttons, for which the mouse is required. All input/output fields have a name linking them to a screen field. The data type of the screen field determines the input format. For example, you cannot enter letters in a numeric field. The screen recognizes when you try to enter invalid values. Radio buttons and checkboxes always have the data type CHAR and length 1. A selected radio button or checkbox has the value ‘X’, when empty, both have the value SPACE.

The ABAP program must contain identically-named data objects that correspond to the screen fields, otherwise data may be lost (see also Linking ABAP Strings to Screen Fields). The ABAP data types that correspond to the screen data types are listed in a table in the Data Types in the ABAP Dictionary section. The ABAP fields for checkboxes and radio buttons must have type C and length 1.

After the PBO event has been processed, the screen fields are filled with the values from the ABAP fields. Before or during the PAI event, the values from the screen fields are written to the ABAP fields. Note that only one radio button within a group can be selected. If more than one of the fields contains the value ‘X’, the program terminates.

To ensure that screen fields and ABAP fields always correspond exactly, use the following procedure.

Local Program Fields

If you want to use input/output fields in one program only, you should create them in the ABAP program, activate the program, and then Structure link copy the fields from the ABAP program to the screen. Afterwards, however, you must avoid changing the fields in the ABAP program.

Screen Fields with Dictionary Reference

If your input/output fields are required in more than one program, and you want to use information from the ABAP Dictionary, such as field labels, field help, and input help, you should Structure link copy fields from the ABAP Dictionary. You can refer to both structures and database tables.

You must then declare identically-named fields as an interface work area in the ABAP program using the TABLES statement. Declaring an identically-named field using a TYPES reference to the data type in the ABAP Dictionary is insufficient for data to be transferred between the screen and the ABAP program.

To avoid naming conflicts between screen fields, work areas in programs, and database tables, it is often worth defining your own ABAP Dictionary structure for screens, containing all of the input/output fields you want to use for one or more screens of a program or for a package. The work areas that you declare with TABLES in the ABAP program then function exclusively as an interface between the program and the screen. If necessary, you can assign values between program fields and the interface work area.

The advantage of referring to ABAP Dictionary data types is that both the screen fields and the fields in the ABAP program are updated automatically if the data type changes.

Example

Local program fields

PROGRAM demo_dynpro_input_output.

DATA: input TYPE i,
output TYPE i,
radio1(1) TYPE c, radio2(1) TYPE c, radio3(1) TYPE c,
box1(1) TYPE c, box2(1) TYPE c, box3(1) TYPE c, exit(1) TYPE c.

CALL SCREEN 100.

MODULE init_screen_100 OUTPUT.
CLEAR input.
radio1 = 'X'.
CLEAR: radio2, radio3.
ENDMODULE.

MODULE user_command_0100 INPUT.
output = input.
box1 = radio1.
box2 = radio2.
box3 = radio3.
IF exit NE space.
LEAVE PROGRAM.
ENDIF.
ENDMODULE.

The next screen number of screen 100 is 100 (statically-defined). Its layout is as follows:

This graphic is explained in the accompanying text

The part of the element list relevant for the screen fields is as follows:

Name

Type

defLg

Format

INPUT

I/O

9

INT4

OUTPUT

I/O

11

INT4

RADIO1

Radio

1

CHAR

BOX1

Check

1

CHAR

RADIO2

Radio

1

CHAR

BOX2

Check

1

CHAR

RADIO3

Radio

1

CHAR

BOX3

Check

1

CHAR

EXIT

Check

1

CHAR

OK

20

OK

The screen fields OUTPUT, BOX1, BOX2, and BOX3 cannot accept user input.

The length of INPUT is such that the user can enter a nine-digit integer without thousand separators. However, the display in the OUTPUT field contains up to two thousand separators. Had we left the length of INPUT at 11 digits, a runtime error could have occurred.

The screen flow logic is as follows:

PROCESS BEFORE OUTPUT.
MODULE init_screen_100.

PROCESS AFTER INPUT.
MODULE user_command_0100.

The entries in the input fields are passed to the ABAP program in the PAI event, and assigned to the output fields in the dialog module USER_COMMAND_100. The next time the screen appears, the screen fields have been filled appropriately. The input fields are set in the PBO dialog module INT_SCREEN_100. If the user selects the screen field EXIT (value ‘X’), the program ends.

Example

Screen fields with Dictionary reference

PROGRAM demo_dynpro_dictionary.

TABLES demo_conn.

DATA wa_spfli TYPE spfli.

CALL SCREEN 100.

MODULE init_screen_100 OUTPUT.
CLEAR demo_conn-mark.
MOVE-CORRESPONDING wa_spfli TO demo_conn.
CLEAR wa_spfli.
ENDMODULE.

MODULE user_command_0100 INPUT.

IF demo_conn-mark = 'X'.
LEAVE PROGRAM.
ENDIF.

MOVE-CORRESPONDING demo_conn TO wa_spfli.

SELECT SINGLE
cityfrom airpfrom cityto airpto fltime deptime arrtime
INTO CORRESPONDING FIELDS OF wa_spfli
FROM spfli
WHERE carrid = wa_spfli-carrid AND connid = wa_spfli-connid.

ENDMODULE.

The statically-defined next screen for screen 100 is 100. It uses components of the structure DEMO_CONN, Structure link copied from the ABAP Dictionary, and looks like this:

This graphic is explained in the accompanying text

The structure DEMO_CONN exists in the ABAP Dictionary specially for screens that use the flight data model. It contains the components of the database table SPFLI, but also a component MARK. MARK uses the domain S_FLAG, which may only have the values SPACE and X. On the above screen, the ABAP Dictionary text for MARK has been overwritten with ‘Cancel’. For all other fields, the ABAP Dictionary texts have been retained. The input attribute of some of the fields has been switched off in the Screen Painter.

Users can enter values for the airline and flight number. Automatic field checks against check tables in the ABAP Dictionary, field help, and input help are automatically available. The field checks are performed automatically before any of the dialog modules in the ABAP program are called. Users cannot enter values for the airline that do not exist in the check table SCARR, values for the flight number that are not in SPFLI, or values for MARK other than SPACE and X. There is no need to program any of these checks in the ABAP program.

The screen flow logic is as follows:

PROCESS BEFORE OUTPUT.
MODULE init_screen_100.

PROCESS AFTER INPUT.
MODULE user_command_0100.

The module USER_COMMAND_100 in the ABAP program reads data from the database table with the key fields specified (and checked) on the screen, and sends them back to the screen in the PBO module INIT_SCREEN_100. The work area DEMO_CONN, defined using the TABLES statement, serves as an interface between the program and the screen. Meanwhile the data from the database is processed in the work area WA_SPFLI. If the user selects the screen field ‘Cancel’ (value ‘X’), the program ends.

Leaving content frame

No comments:

Blog Archive