Skip to main content

Command Palette

Search for a command to run...

Exploring Session State in Oracle APEX with JavaScript

Updated
2 min read

Hello APEX developers! Today, we're exploring how to work with session state using JavaScript in Oracle APEX. We'll cover the basics with easy-to-understand examples and show you both the shortcut and long-form ways to do things.

Reading Session State

To get a value from session state, we use the $v() function or its longer form:

// Shortcut
let username = $v('P1_USERNAME');

// Long form
let email = apex.item('P1_EMAIL').getValue();

console.log("Username: " + username);
console.log("Email: " + email);

Writing to Session State

To set a value in session state, we use $s() or the setValue() method:

// Shortcut
$s('P1_FAVORITE_COLOR', 'Blue');

// Long form
apex.item('P1_FAVORITE_ANIMAL').setValue('Dolphin');

Checking if an Item Exists

Before working with an item, it's smart to check if it exists. We use $x() for this:

// Shortcut
if ($x('P1_PHONE')) {
    console.log("Phone field exists");
}

// Long form
if (apex.item('P1_ADDRESS').node) {
    console.log("Address field exists");
}

Working with Select Lists and Radio Groups

For items based on a List of Values (LOV), APEX provides special functions:

// Get the display value
let deptName = apex.item('P1_DEPARTMENT').displayValueFor(10);
console.log("Department 10 is: " + deptName);

// Set both return and display value (useful for Popup LOVs)
apex.item('P1_EMPLOYEE').setValue(7839, 'KING');

Detecting Changes

To check if an item's value has changed:

if (apex.item('P1_SALARY').isChanged()) {
    console.log("Salary has been modified!");
}

Silent Updates

Sometimes, we want to update a value without triggering events. The setValue method accepts three arguments:

  1. pValue - The value to set

  2. pDisplayValue - The display value

  3. pSuppressChangeEvent - defaults to false, true suppresses the change event

// Set value without firing change event
apex.item('P1_DEPARTMENT').setValue(20, null, true);

Wrapping Up

These JavaScript techniques allow you to create more interactive APEX applications. Remember:

  1. $v() or getValue() to read

  2. $s() or setValue() to write

  3. $x() to check for item existence

Remember...these values aren't set into session state until you submit the page in some fashion!

#orclAPEX