Search This Blog

Monday, June 30, 2025

How to Create and Use APEX CASE Directive with TEMPLATE_TEXT Using {case}, {when}, {otherwise}, and {endcase}

 In Oracle APEX, the CASE directive is a powerful template feature that enables dynamic content rendering based on conditions directly within your HTML templates. Using the CASE directive along with the keywords {case}, {when}, {otherwise}, and {endcase}, developers can implement conditional logic inside templates without writing complex PL/SQL code. This approach allows you to customize how data and UI elements are displayed based on specific values or states, improving the flexibility and user experience of your APEX applications.

To create and use the APEX CASE directive with TEMPLATE_TEXT, you start by embedding the CASE syntax within your HTML or region templates. The structure begins with {case expression}, where the expression is evaluated, followed by one or more {when value} blocks that define the output for matching values. The {otherwise} block specifies a default output if none of the conditions match, and the directive is closed with {endcase}. This simple yet effective logic lets you show different content, styles, or icons based on the data. For example, you might display a green badge for "Active" status and a red badge for "Inactive," all controlled inside the template with CASE directives.

Using the CASE directive enhances maintainability and readability of your templates by centralizing conditional display logic. It reduces the need for multiple static templates or complicated dynamic actions and can be combined seamlessly with other substitution variables. Mastering this directive empowers developers to build highly customizable interfaces that adapt dynamically to underlying data values, providing end-users with clearer visual cues and context-sensitive information.

In Oracle APEX, the CASE directive is a versatile feature used within TEMPLATE_TEXT to add conditional logic to your templates. This directive enables you to control what content or formatting is displayed based on dynamic values, all within the template itself, without needing to write additional PL/SQL code. The directive uses the keywords {case}, {when}, {otherwise}, and {endcase} to define conditions and their corresponding outputs, allowing for clean, maintainable, and powerful template customization.

To create and use the APEX CASE directive, start by embedding the {case expression} at the point in your template where you want to evaluate a variable or column value. The expression inside {case} typically references a substitution string, such as a column name or an item. Following this, you define one or more {when value} blocks. Each {when} block specifies the output to render when the expression matches that particular value. If none of the {when} conditions match, the {otherwise} block defines a default output. The directive is closed with {endcase} to signal the end of the conditional logic.

Here is a basic example to illustrate the syntax:

{case STATUS}
{when ACTIVE}
Active
{when INACTIVE}
Inactive
{otherwise}
Unknown
{endcase}

In this example, the value of STATUS is checked. If it is "ACTIVE", a green "Active" label is shown; if "INACTIVE", a red "Inactive" label; otherwise, a generic "Unknown" label appears. This allows you to create dynamic and visually distinct displays based on data values without complex logic elsewhere.

To implement this in Oracle APEX:

  1. Open your application and navigate to Shared Components.

  2. Select Templates, then choose the template you want to modify, such as a report or card template.

  3. Locate the TEMPLATE_TEXT field where you want to add conditional logic.

  4. Insert the CASE directive syntax with appropriate substitution strings matching your application’s columns or items.

  5. Save your changes and run your page to see the conditional rendering in action.

Using the CASE directive improves maintainability because it consolidates conditional formatting within the template itself, avoiding scattered logic across multiple components or processes. It also enhances user experience by providing clear visual cues directly tied to data states.

The APEX CASE directive with TEMPLATE_TEXT is a powerful tool to add dynamic, condition-based content rendering inside your templates. By mastering the use of {case}, {when}, {otherwise}, and {endcase}, developers can create cleaner templates, reduce code duplication, and build flexible, user-friendly interfaces that respond intelligently to underlying data values.

Example

In Oracle APEX, you can use CASE directives in templates to dynamically generate content based on specific conditions. The syntax {case}, {when}, {otherwise}, and {endcase} are used in APEX templates to create conditional logic, allowing you to show or hide content based on certain values or conditions.

In this tutorial, we’ll demonstrate how to use these directives to display different content dynamically in your APEX application.

Step 1: Understanding the Syntax

Here’s the basic structure of a CASE directive using TEMPLATE_TEXT with {case}, {when}, {otherwise}, and {endcase}:

{case NAME/}

    {when string1/}

        TEMPLATE_TEXT1

    {when string2/}

        TEMPLATE_TEXT2

    {otherwise/}

        TEMPLATE_TEXT

{endcase/}

  • {case NAME/}: This begins the CASE block and is followed by a name or expression. The block will evaluate conditions based on the value of NAME.

  • {when string1/}: This specifies a condition (string1). If NAME matches string1, the content (TEMPLATE_TEXT1) will be displayed.

  • {when string2/}: This specifies another condition (string2). If NAME matches string2, the content (TEMPLATE_TEXT2) will be displayed.

  • {otherwise/}: If no conditions match, this content will be displayed as the default.

  • {endcase/}: This closes the CASE block.

Step 2: Example 1 - Showing Different Content Based on User Role

Suppose you want to display different messages to users based on their roles (e.g., Admin, User, Guest). You can create a CASE directive that evaluates the &APP_USER. (the current user) and shows different text depending on the role.

Steps:

  1. Create a Static Content Region.

  2. In the HTML Expression field, use the following template:

{case &APP_USER./}

    {when 'ADMIN'/}

        <h2>Welcome, Administrator!</h2>

        <p>You have full access to the system.</p>

    {when 'USER'/}

        <h2>Welcome, User!</h2>

        <p>You have limited access to certain features.</p>

    {otherwise/}

        <h2>Welcome, Guest!</h2>

        <p>Please log in to access more features.</p>

{endcase/}

  • Explanation: This block checks the value of &APP_USER. (the logged-in user):

    • If the user is 'ADMIN', it shows a message welcoming the admin.

    • If the user is 'USER', it shows a message for the standard user.

    • If the user is neither, it displays a default guest message.

Expected Output:

If &APP_USER. is 'ADMIN':

Welcome, Administrator!

You have full access to the system.

If &APP_USER. is 'USER':

Welcome, User!

You have limited access to certain features.

If &APP_USER. is any other value (e.g., 'GUEST'):

Welcome, Guest!

Please log in to access more features.

Step 3: Example 2 - Showing Content Based on Page Item Value

In this example, let's say you want to show different content depending on the value selected in a page item (P1_STATUS). For instance, the status could be either 'Active', 'Inactive', or 'Pending'.

Steps:

  1. Create a Page Item: Create a page item called P1_STATUS with possible values: 'Active', 'Inactive', and 'Pending'.

  2. Create a Static Content Region.

  3. In the HTML Expression field, use the following template:

{case :P1_STATUS/}

    {when 'Active'/}

        <h2>Status: Active</h2>

        <p>The system is running smoothly.</p>

    {when 'Inactive'/}

        <h2>Status: Inactive</h2>

        <p>The system is currently inactive. Please contact support.</p>

    {when 'Pending'/}

        <h2>Status: Pending</h2>

        <p>The system status is pending. Please wait for further updates.</p>

    {otherwise/}

        <h2>Status: Unknown</h2>

        <p>The status could not be determined.</p>

{endcase/}

  • Explanation: This block evaluates the value of the P1_STATUS page item:

    • If P1_STATUS is 'Active', it shows a message indicating that the system is running smoothly.

    • If P1_STATUS is 'Inactive', it shows a message about the system being inactive.

    • If P1_STATUS is 'Pending', it shows a message about the pending status.

    • If none of these values are selected, it shows a default message.

Expected Output:

If P1_STATUS is 'Active':

Status: Active

The system is running smoothly.

If P1_STATUS is 'Inactive':

Status: Inactive

The system is currently inactive. Please contact support.

If P1_STATUS is 'Pending':

Status: Pending

The system status is pending. Please wait for further updates.

If P1_STATUS is any other value:

Status: Unknown

The status could not be determined.

Step 4: Example 3 - Customizing Output Based on a Session Variable

In this example, let’s use a session variable to customize content dynamically. You can create a condition that checks whether the session variable SESSION_STATUS is 'LOGGED_IN' or 'LOGGED_OUT' to display different messages.

Steps:

  1. Set a session variable: Set the SESSION_STATUS session variable to 'LOGGED_IN' or 'LOGGED_OUT' based on the user’s login state.

  2. Create a Static Content Region.

  3. In the HTML Expression field, use the following template:

{case :SESSION_STATUS/}

    {when 'LOGGED_IN'/}

        <h2>Welcome Back!</h2>

        <p>You're logged in and ready to go.</p>

    {when 'LOGGED_OUT'/}

        <h2>Logged Out</h2>

        <p>You have been logged out. Please log in again to continue.</p>

    {otherwise/}

        <h2>Status Unknown</h2>

        <p>Your session status could not be determined. Please try again later.</p>

{endcase/}

  • Explanation: This block evaluates the session variable SESSION_STATUS:

    • If SESSION_STATUS is 'LOGGED_IN', it shows a welcome message.

    • If SESSION_STATUS is 'LOGGED_OUT', it shows a message indicating the user is logged out.

    • If neither condition is met, it shows a default "status unknown" message.

Expected Output:

  • If SESSION_STATUS is 'LOGGED_IN':

  • Welcome Back!

  • You're logged in and ready to go.

  • If SESSION_STATUS is 'LOGGED_OUT':

  • Logged Out

  • You have been logged out. Please log in again to continue.

  • If SESSION_STATUS has any other value:

  • Status Unknown

  • Your session status could not be determined. Please try again later.

Step 5: Using Dynamic Content with CASE Directive in Regions

You can dynamically change the content of entire regions based on conditions. The CASE directive can be used to show or hide entire regions or provide different content within regions depending on the condition being evaluated.

For instance, you can conditionally show a region only for certain user types or based on a condition.

Example Code:

{case :USER_ROLE/}

    {when 'ADMIN'/}

        <h2>Admin Dashboard</h2>

        <p>You have full control over the application settings and users.</p>

    {when 'MANAGER'/}

        <h2>Manager Dashboard</h2>

        <p>You can manage your team's performance and reports.</p>

    {otherwise/}

        <h2>User Dashboard</h2>

        <p>Access basic application features.</p>

{endcase/}

Expected Output:

  • If USER_ROLE is 'ADMIN':

  • Admin Dashboard

  • You have full control over the application settings and users.

  • If USER_ROLE is 'MANAGER':

  • Manager Dashboard

  • You can manage your team's performance and reports.

  • If USER_ROLE is anything else:

  • User Dashboard

  • Access basic application features.

By applying these techniques, you can provide a personalized experience to your users by displaying the right content based on various conditions, ensuring a more dynamic and interactive user interface.

 Key Takeaways:

  • {case NAME/} begins the conditional block and evaluates the value of NAME.

  • {when value/} is used to specify a condition and display content when the condition is met.

  • {otherwise/} defines the default content when no conditions match.

{endcase/} closes the CASE block.

In conclusion, the APEX CASE directive with TEMPLATE_TEXT offers a streamlined and efficient way to embed conditional logic directly within templates. By using {case}, {when}, {otherwise}, and {endcase}, developers can create versatile, responsive user interfaces that adapt based on data values, all while keeping template code clean and maintainable. Leveraging this directive unlocks new possibilities for dynamic presentation in Oracle APEX applications, making it an essential tool for advanced customization.

How to Create and Use APEX If Condition Directives

In Oracle APEX, If Condition Directives are essential tools that allow developers to control the visibility and execution of components based on specific conditions. These directives make it easy to show or hide buttons, regions, items, or even entire processes dynamically, depending on runtime values such as user roles, item values, or session variables. By using these condition types effectively—such as "Item = Value," "PL/SQL Expression," or "SQL Query returns at least one row"—developers can build smarter, more responsive applications that behave differently for different users or states.

In Oracle APEX, If Condition Directives provide a powerful and flexible way to control when components such as regions, items, buttons, and processes are displayed or executed. These directives are conditions defined at the application or component level that evaluate expressions, item values, or user privileges, enabling dynamic behavior and personalization within your application. By leveraging If Condition Directives, developers can ensure that only relevant elements are visible or active based on the current application context, improving both usability and security.

To create and use If Condition Directives in Oracle APEX, begin by navigating to the Application Builder and accessing Shared Components. Under the Logic section, select "User Interface Defaults" or directly go to "If Condition Directives" if available in your version. You can define new condition directives by specifying a unique name and selecting the type of condition. Common types include "Item = Value," where a component is shown or hidden based on an item’s value, "PL/SQL Expression," which allows complex logic using PL/SQL code, and "Authorization Scheme," which restricts components based on user roles or permissions.

Once you create a directive, it can be applied to any APEX component by setting the "Server-side Condition" property and choosing the corresponding directive from the list. For example, to display a button only for users with the ADMIN role, create an If Condition Directive using an Authorization Scheme checking user roles, then assign that directive to the button's server-side condition. Similarly, you can hide regions or disable items based on dynamic conditions, making your application more responsive and secure.

Using If Condition Directives also simplifies maintenance and enhances consistency across your application. Instead of replicating the same logic in multiple components, define the condition once and reuse it wherever needed. This reduces errors and ensures that any changes to the condition are applied application-wide. Additionally, combining If Condition Directives with other APEX features like Dynamic Actions and Authorization Schemes empowers developers to build sophisticated, user-friendly applications that adapt seamlessly to different users and scenarios.

In summary, mastering If Condition Directives in Oracle APEX is essential for building dynamic and secure applications. By defining reusable conditions and applying them effectively to components, developers can control the application’s behavior precisely, improve the end-user experience, and simplify application management. Understanding and implementing these directives unlocks a higher level of customization and efficiency in your Oracle APEX development projects.

Example 

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.

Adding If Condition Directives enhances the user experience by reducing clutter and preventing invalid or unnecessary interactions. It also improves application performance by ensuring that only relevant processes run when needed. Mastering the use of these directives is a key skill in building clean, secure, and personalized Oracle APEX applications that adapt to user context and input.