SAP Business Workflow allows you to seamlessly integrate various business processes, enabling you to carry out multiple tasks at the correct moment. The chronological order and prompt execution of such procedures are guaranteed even when multiple parties are involved.
What makes this all happen is the timely response of the Workflow Management System to the occurrence of predefined events in the SAP system. Event handling is the core of SAP Business Workflow. No matter what your role is in an SAP R/3 implementation, having knowledge of the events and how they are handled is indispensable.
While the details of event creation are beyond the scope of this article, you must familiarize yourself with events as they are interpreted within the SAP landscape and the various elements of SAP that make event processing possible.
Events: Technical background
Events are defined as signals that inform you about the adaptation in the state of a business object. All programs in the SAP system are made aware of the occurrence of an event. Workflow allows any number of programs (event receivers) to respond to a generated event, and those programs may react to the event in their own way.
Workflow steps may be triggered as a reaction to events. When an event is created, workflow steps may be initiated, terminated, or continued. When, for example, an employee's address changes, a workflow automatically sends an e-mail to the employee's supervisor with the new address. Typical examples of events are shown in Figure A.
Events are defined in the Business Object Repository (BOR) as components of an object type. You can define any number of events for a particular object, and you can use the BOR to view a list of events created for a particular object type.
If the standard SAP events don't fulfill your purpose, new events may be added to the given object type. SAP doesn't allow changes to its original object types. It permits changes by using the concept of delegation. Delegation, as the name implies, is a technique of replacing an original object type by its subtype to which additional events may be added.
SAP leverages an elegant set of tools dedicated to event processing. Let's take a look at the various elements of SAP R/3 involved in an event-related scenario.
Event creator programs
Before a program or a workflow task can respond to any event, the event must be created. Programs that generate or create such events are known as event creators, which call the function module SWE_EVENT_CREATE for the creation of events. Let's consider the hiring of an employee as an example. Suppose a program hires an employee and then creates an event named hired via the function module.
Our hiring program may be run for multiple employees simultaneously. The hiring program—when run for each employee—would be creating separate events, each having a distinguishable ID.
The presence of interested receivers is not a rule for event creation. Event creator programs are simply unaware of any possible programs willing to respond to a particular event. The Object key (for example, employee number 123) and the Event ID are important information that the creator program offers to its prospective receivers.
Special care should be taken while writing event creators since it must be ensured that the events aren't generated until the state of an object has actually been changed. In our employee-hiring example, the event—hired—should be generated only when the hiring process has completed without any errors.
Event handling methods or receiver function modules
Event handling methods or receiver function modules are the prospective receivers for a generated event, or, putting it another way, the lines of code executed on the successful creation of an event. These are executed by the Workflow Management System and may either be customer-defined or original SAP programs.
The interface of the event handler is standard and predefined. The event handlers are provided as parameters of the relevant information about an event’s generation. They are as follows:
- Object type: The name of the Object type whose event is generated
- Object key: The unique identification of an object
- Event ID: A number that uniquely identifies an event
- User ID: The user name that generated the event
The linkage table is a database table defining connections between events and their relevant receiver programs. The linkage table is of primary importance in the SAP Business Workflow environment. Depending on the situation, the event linkage table may be populated either by the SAP system or by you.
There are two kinds of linkage tables: type or instance. The important fields of the type linkage table are:
- Object type: This refers to the object type for which the event has been defined (in our case, the employee).
- Event: This denotes the event generated.
- Receiver function module or Event handlers: This refers to the program executed when an event is created.
- Activate/Deactivate Linkage check box: This is used for the purpose of testing or, in the case of exceptional conditions, when the execution of the event handler may not be desirable. You can accomplish this by deactivating the linkage between the event and the receivers. This check box serves this purpose.
When using the workflow builder, the relevant entry is made by SAP in the linkage table. However, for special purposes, you may create or change entries in the linkage table yourself.
This is a subset of the Workflow Management System that serves as the main engine for event processing. On the successful change in state of an object, the event manager detects that an event has been generated. It then searches the event linkage table for any program name linked to the combination of the object type and relevant event.
If such a program is found and the Activate Linkage check box is on, control is transferred to the event handler. The code of the event handler is then executed as a response to the generated event.
Knowledge of events is essential to write even the simplest of workflows. Always follow the golden rule: Before trying to write workflows to solve your business needs, try to express your problem in term of the events that are involved and the necessary responses that you want to be automatically generated.