APEX – prevent a duplicate page submit

Thursday, October 9, 2014

APEX – prevent a duplicate page submit

In one of my projects, I was challenged with the fact that end users (un)intentionally submit a page more than once. As a result, multiple duplicate records are stored in the database. This kind of behavior is unwanted and has to be prevented. Apex provides a feature on page-level to avoid a page from being re-posted, but I didn’t want the end user to be redirected to the error page every time a re-post occurred (see screenshots below).

There are also several solutions on the World Wide Web which involve altering the URL of a button with JavaScript, but I have no desire to modify every submit-button of an application. So I came up with this fairly simple solution which meets my requirements.

Note: This solution makes use of dynamic actions, so it is only compatible with Apex 4 and up.

On page 0 (create this page if you haven’t done it already) create a dynamic action as follows:

  1. Right click on “Dynamic Actions” and select “Create”.

 

  1. Give the dynamic action a name and click “Next”.

  1. Select “Before Page Submit” as event for the dynamic action. Click “Next”.

  1. As action, choose “Execute JavaScript Code”, copy and paste the following code in the Code-field:

    $('a[href="javascript:apex.submit(\''+this.data+'\');"]').attr("href", "JavaScript:void(0);");

    Note:  If your button-template uses a button-tag with the onclick-event instead of a hyperlink, use this JavaScript expression:

    $('button[onclick="apex.submit(\''+this.data+'\');"]').attr("onclick", "JavaScript:void(0);");

    Click “Next”.

  1. Click “Create Dynamic Action”.

This will result in:

Every time the end user clicks on a button which will submit a page, the href/onclick-event of this button will be replaced with “JavaScript:void(0);”. Therefore nothing will happen when the user clicks on the button a second time. As soon as the page is completely refreshed, it can be submitted again.

Simple and effective!

 

VT/JUVO