In Oracle APEX, controlling when components such as regions, buttons, items, or processes display or run is a fundamental part of application design. APEX If Condition directives give you the flexibility to show or hide components based on specific logic—like user roles, page items, or session state. This dynamic behavior enhances the user experience, enforces security, and reduces clutter by ensuring users only see what's relevant to them.
- Understanding If Conditions
If Conditions in APEX are evaluated during page rendering or processing. You can apply them to nearly every component: regions, items, buttons, processes, dynamic actions, validations, and more. - Where to Set If Conditions
- Navigate to a component (for example, a Region).
- Go to the Server-Side Condition or Condition section.
- Choose the type of condition to apply.
- Common Types of Conditions
- Item = Value: Display if a specific item equals a given value.
- Value of Item in Expression 1 = Expression 2: Compare two values dynamically.
- Function Body Returning Boolean: Use PL/SQL logic that returns TRUE or FALSE.
- Current User is in Role: Useful for role-based access control.
- Exists (SQL Query): Show component if a query returns data.
- No Condition: Always show.
- Never: Never show.
- Page is in Printer-Friendly Mode
- Request = Expression 1: Check if the page request matches a value.
- Application Item = Value: Compare application-level session state.
- Example: Hide a Button Unless the User is Admin
- Set the Button’s Server-Side Condition to:
Type: PL/SQL Function Body Returning Boolean
Code:return upper(:APP_USER) = 'ADMIN';
- Example: Only Show a Region if an Item is Checked
- Condition Type: Item = Value
- Item:
P10_SHOW_DETAILS - Value:
Y
- Using Conditional Logic with Authorization Schemes
- Select Type: Current User is Authorized (by Authorization Scheme)
- Pick the desired Authorization Scheme to centralize access logic.
- Use PL/SQL Function Bodies when the condition logic is complex or involves querying tables.
- Reuse logic through Authorization Schemes or Application Items to reduce duplication.
- For performance, avoid complex SQL or PL/SQL conditions that may slow rendering.
- Use simple conditions like
Item = Valuefor better maintainability when appropriate. - Document your condition logic clearly in comments or the component description.
Explore official documentation for full coverage of conditional display logic:
Oracle APEX Conditions Documentation
- : This is the condition you want to evaluate. It can be a comparison between values, checking session variables, or even page item values.
- : The content that will be displayed or action executed if the condition is true.
- <alternative_content>: The content to be displayed or action to be executed if the condition is false (optional).
- #{/if}: This ends the if block.
- Create a Static Content Region on your page.
- In the HTML Expression field, add the following code:
- Explanation: In this example, if the logged-in user (&APP_USER.) is "ADMIN", the region will display a message for the administrator. Otherwise, it will display a generic message for all other users.
- If the logged-in user is "ADMIN":
- Welcome, Administrator!
- You have full access to the application.
- If the logged-in user is not "ADMIN":
- Welcome, JohnDoe!
- Limited access based on your role.
- Create a Page Item: Create a page item (e.g., a checkbox or select list) called P1_SHOW_DETAILS.
- Create a Region: Create a Static Content region where you want to display conditional content.
- In the HTML Expression field, use the following code:
- Explanation: This code checks the value of P1_SHOW_DETAILS (a page item). If it is set to 'Y' (checked or selected), the content in the if block is shown. Otherwise, the else block content is shown.
- If P1_SHOW_DETAILS is set to 'Y':
- Details Section
- This section contains additional information.
- If P1_SHOW_DETAILS is set to 'N':
- Details Section
- This section is hidden because the checkbox is not checked.
- Create an Application Item: Create an application item, e.g., APP_USER_LOGGED_IN.
- Set the Application Item: You can set APP_USER_LOGGED_IN in a login process to 'Y' if the user is logged in.
- Create a Region: Add a Static Content region with the following HTML Expression:
- Explanation: This will display a message to the user based on whether they are logged in or not. The APP_USER_LOGGED_IN application item determines if the user is logged in.
- If APP_USER_LOGGED_IN is 'Y':
- Welcome back, JohnDoe!
- Your previous session was restored.
- If APP_USER_LOGGED_IN is 'N':
- Welcome to the Application
- Please log in to access your data.
- Create a Page Item: Create a page item (e.g., P1_STATUS).
- Create a Dynamic Action: Create a Dynamic Action triggered by a change in P1_STATUS.
- Set the Action Type: Use Execute JavaScript as the action and add the following JavaScript code:
- Explanation: This JavaScript code checks the value of the P1_STATUS page item. If it is set to 'ACTIVE', it shows an alert stating that the status is active. Otherwise, it shows a different message.
- If P1_STATUS is "ACTIVE":
- Alert: "The status is active!"
- If P1_STATUS is not "ACTIVE":
- Alert: "The status is not active."
- Explanation: In this case, the IF condition checks two things: if the user is "ADMIN" and if the page item P1_STATUS is set to 'ACTIVE'. If both conditions are true, the first message will be displayed. Otherwise, the else message will be shown.
Oracle APEX If Condition directives are a core feature for dynamic and secure application behavior. Whether you're tailoring the interface to user roles or managing complex workflows, mastering these conditions ensures that your app responds intelligently to users and context. With careful planning and the use of best practices, If Conditions can greatly enhance both usability and maintainability.