Step by Step Guide to Get APP_USER (User Name) Value From Javascript - Oracle APEX (Codes available here)
In Oracle APEX, it's often necessary to retrieve the logged-in user's name (APP_USER) through JavaScript for various purposes, such as posting the username to endpoints, adding it to URL headers, or performing other user-specific operations. Knowing how to access the APP_USER value directly within the browser session is key to building dynamic, user-centric applications.
This post will walk you through two different methods for retrieving the APP_USER value in Oracle APEX using JavaScript. The first method uses the built-in apex.env.APP_USER
object, while the second method involves creating an Application Item and a Computation to fetch the username dynamically. To watch a video tutorial instead, click below:
Method 1: Using apex.env.APP_USER
Oracle APEX provides a global apex
object that contains useful environment details, including the current session information. One of the easiest ways to retrieve the logged-in user's name is by accessing apex.env.APP_USER
.
Here’s what the apex.env
object looks like:
APEX_BASE_VERSION: "24.1"
APEX_FILES: "https://static.oracle.com/cdn/apex/24.1.3/"
APEX_VERSION: "24.1.3"
APP_FILES: "r/jensphere/app_id/files/static/v6/"
APP_ID: "your app id"
APP_PAGE_ID: "1"
APP_SESSION: "Session Id"
APP_USER: "Your user name (target)"
WORKSPACE_FILES: "r/workspace/files/static/v1/"
Accessing the User Name:
You can easily retrieve the username by running this snippet:
const userName = apex.env.APP_USER;
console.log(userName); // Outputs: 'JENISHJOYAL@GMAIL.COM'
This is particularly useful when you need to post the username to an endpoint or use it in any JavaScript-based operations within your APEX application.
Method 2: Fetching User Information from a Database Table
While the apex.env.APP_USER
method is quick and simple, it is limited to retrieving the username. If you need additional information like user IDs or other custom attributes, it’s better to fetch these from your database. This method involves setting up an Application Item and using it to store values queried from a user table.
Step 1: Create an Application Item
In Oracle APEX, an Application Item acts as a global variable that can store and share values across sessions. Here's how to create one:
- Navigate to Shared Components → Application Items.
- Click on Create and define your Application Item (e.g.,
USER_NAME
orUSER_ID
). - Set the Scope to Global if you want to share the item across multiple applications, or keep it at Application (default).
Pro Tip: If you're using a Global scope, make sure to create the same Application Item in each application that will share the session.
Step 2: Create Application Computation
Next, we need to populate the Application Item with the desired value (e.g., employee ID or name) upon session initiation. To achieve this, we use Application Computation.
- Go to Shared Components → Application Computations.
- Create a new computation and set it to run After Authentication.
- Choose the Computation Type:
SQL Query (return single value)
.
Here’s the DDL and DML to set up the EMPLOYEE
table, which stores user details:
CREATE TABLE EMPLOYEE (
ID NUMBER PRIMARY KEY,
NAME VARCHAR2(100)
);
-- Insert sample data
INSERT INTO EMPLOYEE (ID, NAME) VALUES (1, 'YOUR_USER_NAME');
For the computation, use this query:
SELECT id FROM employee WHERE name = :APP_USER;
Figure 2: Oracle APEX - Application Computation creation
Step 3: Retrieve the Value in JavaScript
Once the Application Item is populated, retrieving its value in your JavaScript code is straightforward. Here’s how you can access both the username and the user ID:
const userName = '&USER_NAME.';
const userId = '&USER_ID.';
console.log(userName); // Outputs the logged-in user name
console.log(userId); // Outputs the user's ID
Conclusion
Both methods offer powerful ways to retrieve the APP_USER
value in Oracle APEX, depending on your requirements. While apex.env.APP_USER
is ideal for simple tasks, using an Application Item and Computation provides more flexibility when you need additional user-specific data from the database.
Feel free to experiment with both approaches based on your project’s needs. The code snippets are provided below for quick use. Happy coding!
Copy the SQL and JavaScript code below:
CREATE TABLE EMPLOYEE (
ID NUMBER PRIMARY KEY,
NAME VARCHAR2(100)
);
INSERT INTO EMPLOYEE (ID, NAME) VALUES (1, 'YOUR_USER_NAME');
SELECT id FROM employee WHERE name = :APP_USER;
const userName = '&USER_NAME.';
const userId = '&USER_ID.';
data:post.title

Written by
Published on October 16, 2024
No comments: