Search This Blog

Saturday, July 12, 2025

How do I ADD a 5 second PL/SQL timer function

 

Introduction
Adding a timer function in PL/SQL can be useful for controlling execution flow, simulating delays, or pacing processes within Oracle APEX applications or database procedures. Implementing a simple 5-second delay allows you to pause execution for a specified time, which can be helpful during testing, throttling resource usage, or synchronizing tasks. Using built-in Oracle functions, you can create an efficient timer without complex coding.

 To add a 5-second timer function in PL/SQL, you can use the built-in DBMS_LOCK.SLEEP procedure. This procedure pauses the execution of your PL/SQL code for a specified number of seconds. It’s a simple and effective way to introduce delays or pauses in your Oracle APEX application processes or database routines.

Here’s how to do it in detail:

Step 1: Use DBMS_LOCK.SLEEP

The DBMS_LOCK.SLEEP procedure accepts a single parameter representing the number of seconds to pause execution. To create a 5-second delay, you simply call:

BEGIN
  DBMS_LOCK.SLEEP(5);
END;

This block causes the session to sleep for 5 seconds before continuing with the next statements.

Step 2: Integrate into Your PL/SQL Code

You can include this sleep call inside any PL/SQL block, procedure, or anonymous block within Oracle APEX, such as:

  • Processes on page submit

  • Dynamic actions invoking PL/SQL code

  • Scheduled jobs or background tasks

Example in a page process:

BEGIN
  -- Your pre-delay logic here

  DBMS_LOCK.SLEEP(5);

  -- Your post-delay logic here
END;

Step 3: Considerations

  • The sleep affects only the current session; other database sessions remain unaffected.

  • Use this carefully in production environments because it holds session resources during the sleep.

  • Avoid using long sleep durations that can cause unnecessary waits or performance issues.

  • For longer or more complex timing, consider other scheduling or asynchronous methods.

Step 4: Testing

  • Add the PL/SQL block with the sleep call to a page process or button action.

  • Run the page and observe the delay before subsequent processing completes.

  • Use timing tools or logs to verify the pause duration.

Adding a 5-second timer in PL/SQL using DBMS_LOCK.SLEEP is a straightforward method to introduce execution delays in Oracle APEX applications or database procedures. This simple function helps control timing in workflows, testing scenarios, or controlled pauses, enhancing your ability to manage process flow effectively.

Adding a 5-Second PL/SQL Timer Function in Oracle APEX

A timer function in APEX can be useful for delaying actions such as refreshing reports, executing processes, or redirecting users. Since PL/SQL executes on the server, implementing a 5-second delay requires using DBMS_LOCK.SLEEP or DBMS_SESSION.SLEEP.


Method 1: Using DBMS_LOCK.SLEEP in a PL/SQL Process

The DBMS_LOCK.SLEEP procedure pauses execution for a given number of seconds.

Steps to Implement

  1. Create a PL/SQL Process

    • In Page Designer, go to Processing > Click Create Process

    • Set Execution Point to After Submit

    • Set Language to PL/SQL

    • Enter the following code:

BEGIN

    DBMS_LOCK.SLEEP(5); -- Wait for 5 seconds

END;

  1. Save and Test

    • Submit the page and observe the 5-second delay before the next action executes.


Method 2: Using JavaScript to Delay a Dynamic Action

If you want the delay to occur without blocking other processes, use JavaScript instead of PL/SQL.

Steps to Implement

  1. Create a Dynamic Action

    • Event: Click (on a button)

    • Action: Execute JavaScript Code

    • Code:

setTimeout(function() {

    apex.submit("SAVE"); // Simulate form submission after 5 seconds

}, 5000);

  1. Save and Test

    • When the button is clicked, APEX will wait 5 seconds before submitting.


Method 3: Using DBMS_SESSION.SLEEP in a PL/SQL Block

DBMS_SESSION.SLEEP is another alternative to DBMS_LOCK.SLEEP but works in anonymous PL/SQL blocks.

Example Usage

BEGIN

    DBMS_SESSION.SLEEP(5); -- Wait for 5 seconds

END;

This can be placed in a Before Header Process if you need to delay page processing.


Best Practices

  • Avoid using PL/SQL sleep functions excessively, as they can block database resources.

  • Use JavaScript for UI-related delays, since it does not block the session.

  • Ensure long delays are necessary, as they can impact user experience.


Adding a 5-second timer in APEX can be done using PL/SQL (DBMS_LOCK.SLEEP, DBMS_SESSION.SLEEP) or JavaScript (setTimeout) depending on whether the delay is needed for server-side or client-side processing.


EXAMPLE:

Create or replace function "SLEEP5SECS" return number as

l_now timestamp := systimestamp;

l_end_time timestamp;

begin

    l_end_time :=  l_now + numtodsinterval (5, 'second');


    while(l_end_time > l_now) loop

        l_now := systimestamp;

    end loop;


    return 0;

end "SLEEP5SECS";

/



Call it from a PL/SQL Source

declare

    lv_resultado number :=0;

begin


    lv_resultado := SLEEP5SECS();

    htp.p(lv_resultado);


end;



A computer screen with white text

Description automatically generated


Conclusion
Implementing a 5-second timer in PL/SQL is straightforward and valuable for managing execution timing in your Oracle APEX applications or database logic. By using Oracle’s DBMS_LOCK.SLEEP procedure, you can pause processing safely and predictably, giving you control over delays without impacting system stability. This technique enhances your ability to build responsive, well-timed workflows.

How do I FILL a Form with data from in an URL

 Introduction
Filling a form with data passed through a URL is a powerful technique in Oracle APEX that enables seamless user experiences by pre-populating form fields based on parameters sent in the URL. This approach is useful for scenarios like editing existing records, passing reference values between pages, or integrating with external systems. By capturing URL parameters and mapping them to page items, developers can streamline workflows and reduce manual data entry.
Filling a form with data from a URL in Oracle APEX involves passing values as URL parameters and then referencing those parameters in your application to pre-populate form fields. This technique is useful when you want to open a form page with certain fields already filled based on external input, such as links, emails, or navigation from other pages.
Here’s a detailed explanation of how to achieve this:
Step 1: Define the Page Items
Ensure your form page has page items that correspond to the data you want to pass and fill. For example, a form with items:
  • P1_NAME (Text Field)
  • P1_EMAIL (Text Field)
Step 2: Pass Parameters via URL
Construct a URL that includes the page number and the items with values as query string parameters.
Format:
f?p=App_ID:Page_ID:Session::NO:RP,PT,PX:item_name:item_value
Example:
f?p=100:1:&SESSION.::NO::P1_NAME,P1_EMAIL:John%20Doe,john.doe@example.com
Here,
  • 100 is the application ID
  • 1 is the page number
  • &SESSION. is the current session token (use your actual session or APP_SESSION)
  • P1_NAME and P1_EMAIL are the page items
  • John Doe and john.doe@example.com are the values (URL-encoded)
When the user accesses this URL, Oracle APEX automatically sets the page items P1_NAME and P1_EMAIL with the specified values before rendering the page.
Step 3: Use the Items in the Form
The form items P1_NAME and P1_EMAIL will display the values passed via the URL automatically when the page loads, allowing the user to see or edit them as needed.Step 4: Additional Handling (Optional)
  • If you need to process or validate these values on page load, you can add computations or processes in APEX.
  • To handle URL parameters dynamically, you can also read them via :APP_USER, :APP_SESSION, or APEX_UTIL.GET_SESSION_STATE if needed.
  • For complex scenarios, you may implement a Before Header process to manipulate or use the parameters.
Step 5: Testing
  • Create a link or button that directs users to the URL with the parameters.
  • Open the URL in a browser.
  • Verify that the form fields are pre-populated with the correct values.
Filling form items from URL parameters in Oracle APEX is a straightforward and effective way to pass data into your application pages. It simplifies user input, supports deep linking, and enhances integration possibilities. By leveraging APEX’s built-in URL syntax for setting page items, developers can create responsive forms that adapt dynamically to external inputs or navigation flows.
Filling a Form with Data from a URL in Oracle APEX
Oracle APEX allows passing values through the URL to populate form fields dynamically. This is useful for pre-filling forms based on user selections or external links.

How URL Parameters Work in APEX
APEX uses the following URL structure to pass values:
f?p=App_ID:Page_ID:Session:Request:Debug:ClearCache:itemNames:itemValues
  • App_ID - The Application ID
  • Page_ID - The Page Number where the form exists
  • Session - The session value (use &SESSION. to auto-populate)
  • Request - Can be used for button processing or custom actions
  • Debug - Debug mode (set to NO or YES)
  • ClearCache - Clears session values (RP for resetting page items)
  • itemNames - The page item(s) to set values for
  • itemValues - The corresponding values for those items

Method 1: Pre-Filling a Form Using URL Parameters
If you have a form on Page 2 with fields P2_NAME and P2_EMAIL, you can pre-fill them using a URL like this:
f?p=100:2:&SESSION.::NO:RP:P2_NAME,P2_EMAIL:John,Doe@example.com
When users visit this URL, the P2_NAME field will be set to John, and the P2_EMAIL field will be set to Doe@example.com.
Steps to Implement
  1. Create a Form Page in APEX with items like P2_NAME and P2_EMAIL.
  2. Ensure these fields have Source set to Only When Current Value is Null to allow URL values.
  3. Share the generated URL for pre-filling the form.

Method 2: Using JavaScript to Read URL Parameters
If you need to process URL parameters dynamically, use JavaScript.
Step 1: Add JavaScript to Your Form Page
Go to Page Designer > Execute When Page Loads, then insert:
function getParameterByName(name) {
    let url = new URL(window.location.href);
    return url.searchParams.get(name);
}

let nameValue = getParameterByName("P2_NAME");
let emailValue = getParameterByName("P2_EMAIL");

if (nameValue) {
    $s("P2_NAME", nameValue);
}
if (emailValue) {
    $s("P2_EMAIL", emailValue);
}
Step 2: Test the URL
Use this URL format to test:
f?p=100:2:&SESSION.::NO:RP:P2_NAME,P2_EMAIL:John,Doe@example.com
This will extract values from the URL and fill the form fields using JavaScript.

Method 3: Using PL/SQL to Process URL Parameters
Sometimes, you may want to process URL parameters before displaying them on the form. You can do this using a Before Header PL/SQL Process.
Steps to Implement
  1. In Page Designer, go to Processing > Before Header.
  2. Create a new PL/SQL Process with this code:
BEGIN
    :P2_NAME := NVL(:P2_NAME, 'Default Name');
    :P2_EMAIL := NVL(:P2_EMAIL, 'default@example.com');
END;
  1. Save and Run the page.
If values are not passed in the URL, the fields will use the default values.

Best Practices
  • Use URL Encoding: If passing values that contain special characters (&, ?, /), encode them using JavaScript’s encodeURIComponent().
  • Security Considerations: Do not pass sensitive information (like passwords) in URLs. Use session state protection for security.
  • Clear Cache if Needed: If a user reloads the page, cached values may persist. Use RP in the URL to reset fields.

Filling a form with data from a URL in Oracle APEX is a simple yet powerful way to enhance user experience. You can use direct URL parameters, JavaScript, or PL/SQL depending on your requirements.

EXAMPLE:
Forms do not operate like a classic report. In order to make a form fill with data you need to create a “Pre-rendering” process that will fire off the loading of data.
In this example we are using the EMPLOYEES table provided with APEX. For our test there is an initiating page that has a link that  has two values:
  • The textbox that we are passing the data into. In this case “P10_EMPNO”.
  • The value that we are passing into that textbox. In this case “EMPNO”.
The code that we are using to generate the link looks something like the following code:
select "EMPNO",
    null LINK_CLASS,
    apex_page.get_url(p_items => 'P10_EMPNO', p_values => "EMPNO") LINK,
    null ICON_CLASS,
    null LINK_ATTR,
    null ICON_COLOR_CLASS,
    case when coalesce(:P9_EMPNO,'0') = "EMPNO"
      then 'is-active' 
      else ' '
    end LIST_CLASS,
    (substr("ENAME", 1, 50)||( case when length("ENAME") > 50 then '...' else '' end )) LIST_TITLE,
    (substr("JOB", 1, 50)||( case when length("JOB") > 50 then '...' else '' end )) LIST_TEXT,
    null LIST_BADGE
from "EMP" x
where (:P9_SEARCH is null
        or upper(x."ENAME") like '%'||upper(:P9_SEARCH)||'%'
        or upper(x."JOB") like '%'||upper(:P9_SEARCH)||'%'
    )
order by "ENAME"

Step 1 – Add a form to an empty page
  • On the body of the page right click and then select Create Region.
A screenshot of a computer

Description automatically generated
  • Make the region a form and name it Form Region
A black and grey striped background

AI-generated content may be incorrect.
  • Select the data table name
A screenshot of a computer

Description automatically generated
Step 2 – Create a process
  • At the Pre-Rendering section right click Create Process
A screenshot of a computer

AI-generated content may be incorrect.
  • Select Type : “Form-Initialization” and select the appropriate form region name
A screenshot of a computer

Description automatically generated
  • Make sure that the Execution is as follows
A screenshot of a computer

Description automatically generated

Step 3 – Set the page Security to “Unrestricted”

A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.


Step 4 – Set the receiving control Security to “Unrestricted”

A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.


That’s it. It should work.

A screenshot of a phone

AI-generated content may be incorrect.

A screenshot of a login

AI-generated content may be incorrect.

 
Conclusion
Using URL parameters to fill forms in Oracle APEX enhances application usability and flexibility. It allows data to flow smoothly between pages or external sources, making the user experience more intuitive and efficient. Mastering this technique enables developers to build dynamic, responsive applications that adapt to user context and improve productivity.

How Do I Make a Faceted Search Map Page in Oracle APEX

Combining faceted search with a map region in Oracle APEX enables users to filter data visually and spatially at the same time. This design ...