In Oracle Forms, buttons are an essential part of user interaction. They allow users to perform specific actions like submitting data, navigating to another form, or executing a custom process with just a click. To handle these actions, Oracle Forms provides the WHEN-BUTTON-PRESSED Trigger. This trigger is one of the most commonly used in form development because it directly responds to user input. In this article, you will learn what the WHEN-BUTTON-PRESSED Trigger is, how it works, its purpose, examples of usage, and best practices to implement it effectively.
What is the WHEN-BUTTON-PRESSED Trigger?
The WHEN-BUTTON-PRESSED Trigger is an item-level trigger in Oracle Forms that fires whenever a user clicks on a button within the form. It is used to define the behavior that should occur after the button is pressed.
Key characteristics:
- It is an item-level trigger and is attached specifically to a button.
- Fires immediately when the button is pressed.
- Can be used to perform navigation, database operations, or call other procedures.
- It does not fire for other items—it is exclusive to button clicks.
Purpose of the WHEN-BUTTON-PRESSED Trigger
The main purpose of this trigger is to define custom functionality for buttons. Since buttons do not have built-in behavior by default, the developer decides what happens when the user presses them.
Common uses include:
- Submitting or committing data to the database.
- Navigating to another block or form.
- Running calculations or reports.
- Opening dialog boxes or showing messages.
- Exiting or closing the form.
- Triggering stored procedures or APIs.
In short, WHEN-BUTTON-PRESSED turns a simple button into a functional action trigger.
When Does the WHEN-BUTTON-PRESSED Trigger Fire?
This trigger fires in a straightforward manner:
- The user clicks on a button.
- The WHEN-BUTTON-PRESSED Trigger fires immediately.
- The PL/SQL code inside the trigger executes.
Unlike some other triggers, there are no complex timing rules—it is directly tied to the user’s click event.
Example of a WHEN-BUTTON-PRESSED Trigger
Here is a basic example of how this trigger can be used:
BEGIN
-- Commit changes to the database
COMMIT;
MESSAGE('Data saved successfully.');
MESSAGE(' ');
END;
Explanation:
- When the button is pressed, all pending changes in the form are committed to the database.
- A confirmation message is displayed to the user.
Example: Navigation with a Button
Another common use is navigating to another block or form:
BEGIN
-- Navigate to the Orders block
GO_BLOCK('ORDERS');
EXECUTE_QUERY;
END;
In this example:
- The button takes the user to the
ORDERSblock. - A query is executed to fetch the records automatically.
Example: Calling Another Form
Developers often use buttons to switch forms in multi-form applications.
BEGIN
-- Call another form
CALL_FORM('CUSTOMER_FORM');
END;
This code allows the user to move from the current form to the CUSTOMER_FORM.
Best Practices for WHEN-BUTTON-PRESSED Trigger
To ensure effective use of this trigger, keep the following best practices in mind:
- Keep code modular
- Instead of writing long blocks of code inside the trigger, call stored procedures or program units for better maintainability.
- Provide user feedback
- Display meaningful messages after an action (e.g., data saved, navigation complete).
- Avoid unnecessary commits
- Commit only when required to avoid frequent writes to the database.
- Handle errors gracefully
- Use proper exception handling to manage database or navigation errors.
- Do not overload the trigger
- A button should perform one clear action. If multiple actions are required, ensure they are logically grouped.
Common Mistakes to Avoid
Some mistakes developers often make with WHEN-BUTTON-PRESSED include:
- Placing too much code inside the trigger: Makes maintenance difficult.
- Using it for automatic processes: This trigger is user-driven, not meant for background automation.
- Skipping error handling: Without proper exception handling, failures may confuse the user.
- Over-committing data: Frequent commits can harm performance and data consistency.
Benefits of Using WHEN-BUTTON-PRESSED Trigger
When used properly, this trigger offers several benefits:
- Provides direct user control over actions.
- Enhances usability by making buttons functional and interactive.
- Simplifies navigation across forms and blocks.
- Improves system efficiency by letting users trigger actions only when needed.
- Allows developers to customize application flow in a flexible way.
Conclusion
The WHEN-BUTTON-PRESSED Trigger in Oracle Forms is one of the most widely used triggers because it enables developers to define exactly what should happen when a user clicks a button. From saving data and navigating between blocks to calling other forms or reports, this trigger gives full flexibility to control form behavior.
By keeping the code modular, providing user feedback, and following best practices, developers can make Oracle Forms applications more interactive, user-friendly, and efficient. The WHEN-BUTTON-PRESSED Trigger is a cornerstone of event-driven programming in Oracle Forms, making it essential for building practical business applications.

