🚀Make Your APEX App Smarter with User Preferences
🚩Introduction
In real-world Oracle APEX applications, delivering a personalized user experience is essential for improving usability and efficiency. Different users often have different preference - such as the number of rows displayed in a report, default filter values, or UI settings like theme and layout. Instead of forcing all users to work with the same defaults, Oracle APEX provides a built-in mechanism called User Preferences to store and retrieve user-specific settings.
User preferences allow developers to save small pieces of information for each user and reuse them across sessions. This helps create applications that feel more dynamic and tailored without requiring complex database designs. By leveraging user preferences, developers can significantly enhance user satisfaction while keeping the application logic simple and maintainable.
📑 Why This Approach Is Needed
In Oracle APEX applications, users interact with the system in different ways based on their roles, habits, and requirements. A one-size-fits-all interface often leads to inefficiency and a poor user experience.
Using user preferences becomes important when:
- Users want to customize how much data they see (e.g., rows per page)
Default values in forms should be remembered for each user
Filters and report settings need to persist across sessions
UI settings like theme, layout, or language should be user-specific
Applications need to provide a personalized experience without extra tables
This approach ensures flexibility, reduces repetitive user actions, and improves overall usability.
⚙️ What are User Preferences?
User preferences in Oracle APEX are key–value pairs stored for each user. These values are saved internally by APEX and can be accessed anytime during a session or even across sessions.
Each preference includes:
- User Name
- Preference Name
- Preference Value
These preferences are lightweight and ideal for storing small pieces of user-specific data.
👉 Use Case: Personalizing UI Using User Preferences
Step 1:
Step 2:
Step 3:
Now create the dynamic action for the button of type Click event and create the true action of type Execute Javascript Code and call the following javascript function.
switchTheme();//Store the User Preference value using SET_PREFERENCE
APEX_UTIL.SET_PREFERENCE(
p_preference => 'USER_PREF', -- Your own name for refering the value
p_value => :P35_USER_PREF, -- Hidden Page item
p_user => :APP_USER
); Step 4:
Now while the page is loading at the initial stage, we need to retrieve the user preference and we need to set the theme. To do this, create the process in the pre-rendering and copy the following code.
:P35_USER_PREF := APEX_UTIL.GET_PREFERENCE(
p_preference => 'USER_PREF',
p_user => :APP_USER
);we are using the APEX_UTIL.GET_PREFERENCE package to retrieve the user preference value and storing the value in the Hidden page item.
Step 5:
Now as a final step, we need to add the required CSS and javascript code, to switch the themes dynamically. Copy and paste the below javascript code in the page Function and Global Variable Declaration section and CSS code in the Inline CSS section.
//Function and Global Declaration Section
function applyTheme(theme) {
var btn = document.getElementById("BTN_THEME"); // Static ID of Button
var icon = btn.querySelector("span.t-Icon");
if (theme === "dark-mode") {
document.body.classList.add("dark-mode");
icon.classList.remove("fa-moon-o");
icon.classList.add("fa-sun-o");
} else {
document.body.classList.remove("dark-mode");
icon.classList.remove("fa-sun-o");
icon.classList.add("fa-moon-o");
}
apex.item("P35_USER_PREF").setValue(theme);
}
function switchTheme() {
var currentTheme = document.body.classList.contains("dark-mode") ? "dark-mode" : "light-mode";
var newTheme = (currentTheme === "dark-mode") ? "light-mode" : "dark-mode";
applyTheme(newTheme);
}
document.addEventListener("DOMContentLoaded", function() {
var userPref = apex.item("P35_USER_PREF").getValue();
if (userPref === "dark-mode" || userPref === "light-mode") {
applyTheme(userPref);
} else {
var prefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
applyTheme(prefersDark ? "dark-mode" : "light-mode");
}
});/* Inline CSS Section */
/* Default (Light Theme) */
:root {
--bg-color: #ffffff;
--text-color: #000000;
--primary-color: hsl(210, 100%, 40%);
--btn-bg-color: hsl(210, 100%, 40%);
--btn-text-color: #ffffff;
}
/* Dark Theme */
.dark-mode {
--bg-color: #121212;
--text-color: #ffffff;
--primary-color: #4dabf7;
--btn-bg-color: #1e1e1e;
--btn-text-color: #ffffff;
}
/* Apply variables */
body {
background-color: var(--bg-color);
color: var(--text-color);
}
a {
color: var(--primary-color);
}
.t-HeroRegion {
background-color: #e6e6e6;
}
#BTN_THEME {
background-color: var(--btn-bg-color);
color: var(--btn-text-color);
border: none;
padding: 0.5em 1em;
cursor: pointer;
border-radius: 4px;
transition: background-color 0.3s, color 0.3s;
}
button:hover {
opacity: 0.85;
}
Step 6:
Now save and run the page, you will see the button rendered in the page, clicking on the page, it will toggle the theme between light / dark mode and stores the value. Once when you logged out and login again the stored theme will be retrieved and theme will be set respectively.
🔥 Conclusion
APEX_UTIL.GET_PREFERENCE and APEX_UTIL.SET_PREFERENCE APIs without relying on any external plugins. This approach allows us to store preferences such as theme selection (dark/light mode) at the user level and retrieve them seamlessly across sessions. This can deliver a personalized user experience while keeping the implementation simple, maintainable, and fully integrated within the APEX framework.








Comments
Post a Comment