Search This Blog

Sunday, July 13, 2025

How to Create and Use APEX If Condition Directives

 
Introduction
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.
How to Create and Use APEX If Condition Directives
  1. 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.
  2. 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.
  3. 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.
  4. 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';
  5. Example: Only Show a Region if an Item is Checked
    • Condition Type: Item = Value
    • Item: P10_SHOW_DETAILS
    • Value: Y
  6. Using Conditional Logic with Authorization Schemes
    • Select Type: Current User is Authorized (by Authorization Scheme)
    • Pick the desired Authorization Scheme to centralize access logic.
Best Practices
  • 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 = Value for better maintainability when appropriate.
  • Document your condition logic clearly in comments or the component description.
Official Oracle APEX Documentation
Explore official documentation for full coverage of conditional display logic:
Oracle APEX Conditions Documentation
In Oracle APEX, If condition directives are used to dynamically control the behavior and content of your pages based on certain conditions. They allow you to use conditional logic (similar to IF statements in programming) directly in your APEX application. These directives enable you to show or hide regions, items, or perform specific actions based on values like the current user, session, or specific conditions within your data.
The If condition directives are often used in regions, dynamic actions, and SQL queries. In this tutorial, we will cover the basics of these directives and provide practical examples of how to use them in Oracle APEX.

Step 1: Understanding If Condition Directives
APEX If condition directives allow you to check conditions dynamically and display or execute content based on the evaluation of those conditions.
Syntax of the IF Directive
The syntax of the IF directive in APEX is:
#{if <condition>} 
    <content>
#{else} 
    <alternative_content>
#{/if}
  • : 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.

Step 2: Using IF Condition Directives in APEX
Let’s explore a few practical examples of how to use If condition directives in various scenarios in Oracle APEX.

Example 1: Showing Content Based on Session Variables
You can use the IF directive to show different content based on the session variables or page items, such as displaying a personalized message for the logged-in user.
Steps:
  1. Create a Static Content Region on your page.
  2. In the HTML Expression field, add the following code:
#{if &APP_USER. == 'ADMIN'}
    <h2>Welcome, Administrator!</h2>
    <p>You have full access to the application.</p>
#{else}
    <h2>Welcome, &APP_USER.!</h2>
    <p>Limited access based on your role.</p>
#{/if}
  • 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.
Expected Output:
  • 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.

Example 2: Show or Hide Regions Based on Item Value
You can also use If condition directives to conditionally show or hide regions based on a page item value.
Steps:
  1. Create a Page Item: Create a page item (e.g., a checkbox or select list) called P1_SHOW_DETAILS.
  2. Create a Region: Create a Static Content region where you want to display conditional content.
  3. In the HTML Expression field, use the following code:
#{if :P1_SHOW_DETAILS == 'Y'}
    <h3>Details Section</h3>
    <p>This section contains additional information.</p>
#{else}
    <h3>Details Section</h3>
    <p>This section is hidden because the checkbox is not checked.</p>
#{/if}
  • 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.
Expected Output:
  • 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.

Example 3: Conditional Display of Regions Based on Application Item
You can conditionally display regions based on application items, such as checking whether a user is logged in or not.
Steps:
  1. Create an Application Item: Create an application item, e.g., APP_USER_LOGGED_IN.
  2. Set the Application Item: You can set APP_USER_LOGGED_IN in a login process to 'Y' if the user is logged in.
  3. Create a Region: Add a Static Content region with the following HTML Expression:
#{if :APP_USER_LOGGED_IN == 'Y'}
    <h2>Welcome back, &APP_USER.!</h2>
    <p>Your previous session was restored.</p>
#{else}
    <h2>Welcome to the Application</h2>
    <p>Please log in to access your data.</p>
#{/if}
  • 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.
Expected Output:
  • 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.

Example 4: Using IF Directive in Dynamic Actions
You can also use the IF condition within Dynamic Actions to execute actions based on conditions, such as page item values or session states.
Steps:
  1. Create a Page Item: Create a page item (e.g., P1_STATUS).
  2. Create a Dynamic Action: Create a Dynamic Action triggered by a change in P1_STATUS.
  3. Set the Action Type: Use Execute JavaScript as the action and add the following JavaScript code:
if ("#{P1_STATUS}" == "ACTIVE") {
    alert("The status is active!");
} else {
    alert("The status is not active.");
}
  • 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.
Expected Output:
  • If P1_STATUS is "ACTIVE":
  • Alert: "The status is active!"
  • If P1_STATUS is not "ACTIVE":
  • Alert: "The status is not active."

Step 3: Advanced Conditional Logic
You can combine multiple conditions using AND, OR, and other logical operators to create more advanced logic within your IF directives.
Example: Check Multiple Conditions
#{if &APP_USER. == 'ADMIN' && :P1_STATUS == 'ACTIVE'}
    <p>Welcome back, Admin. The status is active!</p>
#{else}
    <p>Either you are not an Admin, or the status is not active.</p>
#{/if}
  • 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.

By using these directives, you can create flexible and responsive applications that adapt to different user interactions, session states, or data values.
 
Conclusion
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.

No comments:

Post a Comment

How Do I Make a Faceted Search Map Page in Oracle APEX

Combining faceted search with a map region in Oracle APEX enables users to filter data visually and spatially at the same time. This design ...