Utilizing Application Items and Application processes for base URL'S and constant values
Oracle APEX offers developers the flexibility to use Application Items and Application Processes to manage reusable values and execute logic at an application-wide level. One practical use case is dynamically constructing URLs by storing the base domain as an application item and referencing it in processes. This guide provides an overview and examples to help you understand and implement these features effectively.
What Are Application Items?
Application Items are global variables in Oracle APEX that are accessible throughout the application. They are ideal for storing values such as:
Configuration settings (e.g., base URLs, API keys)
User-specific or session-specific values
What Are Application Processes?
Application Processes are pieces of PL/SQL code that execute at specific points during the application lifecycle. They are often used for:
Data preparation
Shared business logic
Reusable operations
Step-by-Step Implementation: Storing and Using Base URLs
1. Creating an Application Item
To store the base URL:
Navigate to Shared Components > Application Items.
Click Create and define the application item:
Name:
BASE_DOMAIN
Value:
https://example.com
(default value for your base URL)
2. Creating an Application Process
To define reusable logic:
Go to Shared Components > Application Processes.
Click Create and configure the process:
Name:
Send_Notification
Point: On Demand Process (for AJAX calls)
PL/SQL Code:
DECLARE
l_full_url VARCHAR2(4000);
l_recipient_email VARCHAR2(100) := 'user@example.com';
BEGIN
-- Construct the full URL using the application item
l_full_url := :BASE_DOMAIN || '/send_notification?email=' || l_recipient_email;
-- Example: Use UTL_HTTP to send a GET request
UTL_HTTP.REQUEST(l_full_url, 'GET');
-- Log the URL for debugging
APEX_DEBUG.MESSAGE('Notification URL Called: ' || l_full_url);
END;
3. Calling the Application Process
In your APEX page:
- Use a Dynamic Action to call the process via AJAX when a button is clicked.
apex.server.process("Send_Notification", {
x01: "User Email"
}, {
success: function(data) {
apex.message.alert("Notification sent successfully!");
},
error: function(error) {
apex.message.alert("Error sending notification: " + error.responseText);
}
});
Example 2: Storing API Keys and Constructing API Calls
1. Storing the API Key
Create an application item called
API_KEY
.Set its value to your API key:
123456789ABCDEFG
.
2. Using the API Key in a Process
DECLARE
l_full_url VARCHAR2(4000);
l_api_key VARCHAR2(100);
BEGIN
-- Fetch the API key from the application item
l_api_key := :API_KEY;
-- Construct the API endpoint URL
l_full_url := :BASE_DOMAIN || '/fetch_user_data?key=' || l_api_key;
-- Log the constructed URL
APEX_DEBUG.MESSAGE('API URL: ' || l_full_url);
-- Example logic: Fetch data from the API (using DBMS_OUTPUT here as a placeholder)
DBMS_OUTPUT.PUT_LINE('Calling API: ' || l_full_url);
END;
Example 3: Dynamically Changing the Base URL per Environment
Problem
Your application is deployed across multiple environments (e.g., Development, Testing, Production), and each environment has a different base URL.
Solution
Set the Application Item Dynamically:
- In the Initialization PL/SQL Code under Shared Components > Application Definition > Initialization Code:
BEGIN
IF :APP_USER = 'DEV_USER' THEN
:BASE_DOMAIN := 'https://dev.example.com';
ELSIF :APP_USER = 'TEST_USER' THEN
:BASE_DOMAIN := 'https://test.example.com';
ELSE
:BASE_DOMAIN := 'https://prod.example.com';
END IF;
END;
Reference the Base URL in Processes:
- Use
:BASE_DOMAIN
as shown in earlier examples.
- Use
Example 4: Using Application Items in Page Processes
Scenario
You want to use the base URL stored in the application item within a page process to redirect the user to an external URL.
Implementation
BEGIN
-- Redirect to the external URL
owa_util.redirect_url(:BASE_DOMAIN || '/welcome_page');
END;
This process could be triggered, for instance, when a user logs in.
I hope it was useful please do follow me here at hashnode for further Apex related topics. Thanks for the read.