This blog is about how to use an Oracle APEX Dynamic Action to display a JavaScript (JS) confirm message and then run a subsequent PL/SQL block if confirmed.
[su_spacer size=”30″]Example Scenario
Imagine that we have an Interactive Report of Employees. To make the user experience better, we add a trash can icon to each row that will call a delete function (See this blog).
To enhance the UI more, we want to make the confirm box show the name of the employee when the trash can icon is clicked. When the delete action is confirmed, then we want to run the PL/SQL code to delete the employee selected and refresh the Interactive Report.
[su_spacer size=”30″]Standard Confirm Action Won’t Work
We cannot just add a Confirm Action to a Dynamic Action because the name (and id) of our employee will change based on the person selected in the Interactive Report. If we try to use a substitution string in the Confirm Action text for the employee name, it will only be the value of employee name page item on page load.
Would you like to delete &P2_EMP_NAME.?
[su_spacer size=”30″]
APEX JavaScript Confirm Action Won’t Work
If we follow the Oracle APEX JavaScript API and use a JavaScript block in a Dynamic Action for the confirm box, it will display the name properly, but it brings up another problem. This is because the PL/SQL block or any other thing in the Dynamic Action after the JavaScript Confirm box will fire regardless of if the user clicks Ok, Cancel, or closes the popup.
Implementing the APEX JavaScript confirm message in a different way will give us more flexibility, but it would take many more steps to allow us to execute PL/SQL from JavaScript.
apex.message.confirm("Do you want to delete " + $v("P2_EMP_NAME") + "?", function( okPressed ){
if( okPressed ) {
}
});
[su_spacer size=”30″]
The Solution
[su_spacer size=”30″]In order to implement this in the easiest way possible, we need to do the following:
- Create a Dynamic Action
- Add an Execute JavaScript Action and use the code below
return confirm("Are you sure you want to delete " + $v("P2_EMP_NAME") + "?");
- Then add an Execute PL/SQL Code Action for the delete statement
delete from emp
where emp_id = :p2_emp_id
- Finally, add a Refresh Action on the Interactive Report region
Why Does That Work?
The standard JavaScript of Return Confirm() prevents any subsequent Dynamic Actions from firing unless the user clicks Ok.
[su_spacer size=”30″]Learn More
- You can learn more helpful tips like the ones above by visiting the Traust Consulting Blog.
Disclaimer:
We do not take responsibility for any unintended or unwanted consequences in your instance of Oracle, Oracle APEX, or related products as a result of reading our blogs or following our guides. Though the information is fully tested and generally safe to use, our lawyers really have a thing against admitting potential wrongdoing. If it makes you feel any better, one time I convinced them that the term “Mofo” was the new cool way of saying mobile phone. I’m pretty sure we set a new record for most contracts lost in one afternoon.