Search This Blog

Monday, June 30, 2025

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

Adding a delay or timer function in Oracle APEX using PL/SQL can be useful in various scenarios, such as simulating processing time, pacing background jobs, or controlling the flow of certain operations. One common requirement is to implement a simple 5-second pause in your PL/SQL code. This can be achieved efficiently using built-in PL/SQL features, enabling you to introduce timed waits without impacting overall application performance.

In Oracle APEX, adding a 5-second timer function using PL/SQL can be done primarily with the built-in DBMS_LOCK.SLEEP procedure. This procedure pauses the execution of your PL/SQL code for a specified number of seconds, allowing you to introduce a delay or wait period as part of your application logic.

Steps to Add a 5-Second Timer Using PL/SQL in Oracle APEX

  1. Identify where to add the timer:
    The timer can be added inside any PL/SQL block, such as a process, a computation, a validation, or a dynamic action that executes PL/SQL code.

  2. Use the DBMS_LOCK.SLEEP procedure:
    The syntax is simple:

    DBMS_LOCK.SLEEP(seconds => 5);
    

    This command tells Oracle to pause the execution for 5 seconds.

  3. Example of adding a 5-second delay in a Process:

    • Go to your APEX application and open the Page Designer.

    • Under the “Processing” section, create a new Process or edit an existing one where you want to add the delay.

    • In the PL/SQL Code section, add the following code snippet:

    BEGIN
       -- Your logic before the delay
       DBMS_LOCK.SLEEP(5); -- Pause for 5 seconds
       -- Your logic after the delay
    END;
    

    This will cause the process to wait 5 seconds before continuing.

  4. Using the Timer in Dynamic Actions:
    You can also create a Dynamic Action that executes PL/SQL code and includes the sleep timer to control the timing of actions on the page.

  5. Considerations:

    • Avoid using unnecessary delays in UI-interactive code to maintain a responsive user experience.

    • Using DBMS_LOCK.SLEEP in server-side processes is best suited for scenarios such as throttling background jobs or simulating wait times during development.

    • Ensure the user is aware if a delay affects the UI, possibly using loading indicators.

By using the DBMS_LOCK.SLEEP function, you can precisely control execution timing in Oracle APEX, making it easy to add pauses like a 5-second timer in your PL/SQL code blocks.

Example

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

Incorporating a 5-second timer in your PL/SQL code within Oracle APEX is straightforward and can enhance your application's control flow where timing is essential. By using simple techniques like DBMS_LOCK.SLEEP, you can add precise delays that help manage processing or user interactions smoothly. This approach keeps your application responsive while meeting timing requirements effectively.

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

 Using Oracle APEX, you can fill a form with data passed through URL parameters, enabling dynamic and context-sensitive page behavior. This technique allows developers to pre-populate form fields based on values included in the URL, streamlining user input and improving navigation between pages. By capturing these parameters and mapping them to form items, you create a seamless experience where forms are automatically filled with relevant data upon page load, reducing manual entry and potential errors.

To fill a form with data from a URL in Oracle APEX, you leverage URL parameters to pass values into page items, which are the fields on your form. This method allows you to pre-populate form fields dynamically when the page loads, improving user experience by reducing manual data entry and enabling seamless navigation between related pages.

Steps to Fill a Form with Data from a URL:

  1. Identify the Form Page and Items
    First, determine which page contains the form you want to populate. Note the page number and the names of the form items (e.g., P10_FIRST_NAME, P10_LAST_NAME) you want to fill.

  2. Construct the URL with Parameters
    Create a URL that passes values to these form items using the syntax:
    f?p=App_ID:Page_ID:Session_ID::Request:Debug:ClearCache:item_names:item_values
    For example, to fill the form on page 10 with first name “John” and last name “Doe”:

    f?p=100:10:&SESSION.::NO::P10_FIRST_NAME,P10_LAST_NAME:John,Doe
    

    Here, 100 is the application ID, 10 is the form page number, and &SESSION. is a substitution for the current session ID.

  3. Configure Page Items to Accept URL Values
    Ensure the form page items are set to “Source” type “Always, replacing any existing value.” This allows the page items to accept and display the values passed via URL every time the page is loaded.

  4. Use Branches or Processes if Needed
    If additional processing or validation is required based on the passed data, use page processes or branches to handle that logic during page load or submission.

  5. Test the URL
    Navigate to the constructed URL in your browser. The form should open with the specified fields pre-filled with the values passed in the URL.

Additional Tips:

  • Make sure item names in the URL exactly match the page item names.

  • Use URL encoding if parameter values contain special characters or spaces.

  • This technique is often used in master-detail pages or when linking from summary reports to detailed edit forms.

By using URL parameters to fill form fields, you provide users with a faster, more intuitive workflow, reducing errors and improving overall application usability.

Example

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.

In conclusion, filling a form with data from a URL in Oracle APEX is a powerful method to enhance user interaction and application efficiency. By leveraging URL parameters and page item mappings, developers can create responsive forms that adapt to different scenarios and inputs. This approach not only saves time but also ensures data consistency and improves overall user satisfaction within the application.

HOW DO I SET A HOME PAGE

 Setting a home page in Oracle APEX is an essential step in defining the default landing page for your application. The home page serves as ...