Search on this Website

Saturday, December 1, 2007

Event-Driven Programming -Selection Screens

In this day and age, with the emergence of the World Wide Web and all that it entails, program code must be capable of interacting and communicating with the end user. This is done in ABAP/4 using events that are invoked by the users' actions.

Processing blocks are defined by event keywords and are thus executed on the invocation of certain relevant events.


By default, the event start-of-selection is attached to all events in ABAP/4. In your programs you can define a processing block and attach this block to an event keyword.

For generally easy-to-read code, it is good practice to define sequential processing blocks in the order by which they will most likely be triggered during selection screen execution. It is also good practice to utilize the most important events for selection screen programming. These events are as follows:

(i)The initialization event
(ii)The at selection-screen event
(iii)The at user-command event

Using the initialization Event


The following code shows the syntax for the initialization event:

report ywhatyr.
tables: marc, mvke.
..
data: p_year for sy-datum.
initialization.
if sy-datum ge '01012000'
p_year = '2000'.
else.
p_year = 'Yesteryear'.
endif.

In this example, when the program executes for which a selection screen is defined, this initialization processing block is executed, setting the parameter field p_year equal to a value depending on the system date at the moment of execution. It is this block in which you specify the initial default values of your selection screen based on whatever criteria is necessary to maintain data integrity of user input. Some examples include setting title bars, assigning text elements to graphical user interface (GUI) elements, and function code status.

You have seen how the initialization event works, so now it's time to take a closer look at some uses of the at selection-screen and at user-command events.

Using the at selection-screen Event
The at selection-screen event is processed after user input on the active selection screen. This can occur when the user presses a function key or clicks a pushbutton, as well as a host of other elements that can be interacted on by the user. In addition to data validation checks, warning messages, GUI status change, or even pop-up windows can be called using the at selection-screen event. You will look at more examples of these later in the chapter when you look at formatting selection screens and the events involved with screen formatting elements. For now, however, let's take a look at a pushbutton on a selection screen and how the events at selection-screen and at user-command are used with this next example.

Using the at user-command Event
Pushbuttons, as well as many other event-driven selection screen options, can be very useful in maintaining user interaction and validating user input. In this next section, you will explore how to use pushbuttons to invoke the at user-command event and look at an example of how pushbuttons can be used to process user input.

Syntax for the selection-screen pushbutton Event
The following code shows the syntax for the selection-screen pushbutton event:

selection-screen pushbutton example1 user-command 1234.

This statement, when used together with the at selection-screen command, is a great way to interact with the user as he enters data. The syntax is similar to that of a selection-screen comment except that data is passed when the user presses the button. Pushing the button triggers sccrfields-ucomm in the at selection-screen event and the input fields are imported. This data can then be validated and the user issued a message depending on the purpose of the button. This is an example of how you can use two pushbuttons to determine which language to report selected data in.

The following code shows the syntax for two pushbuttons that are used to choose a language:

selection-screen pushbutton 10(20) text-003 user-command engl.
selection-screen pushbutton 50(20) text-004 user-command germ.

at selection-screen.
at user-command.
case sy-ucomm.
when 'engl'.
lang-english = 'Y'.
when 'germ'.
lang-german = 'Y'.
endcase.

In this example, you can check which of the two pushbuttons were pressed by the user by using a case statement (see Figure 21.1). When the user triggers the at user-command event, the field sy-ucomm holds the unique four-byte name of the item that the user selected. In this way, you can code various data validations or command user input based on the combination of data entered and the items, in this case pushbuttons, selected by the user.

Now that you have learned a little about data validation using events in ABAP/4, it is time to look at some data validation techniques that are more system maintained and defined. You will first look at foreign keys. A foreign key is one or more fields that represent the primary key of a second table.

No comments: