When you run APEX 20.2 or later, you will notice a couple of differences to previous versions. For example, the “Execute PL/SQL Code” Dynamic Action has been renamed to a more generic “Execute Server-side Code” and the “Execute PL/SQL Code” Process has been renamed to just “Execute Code”.
As this example shows, you can interact with page items via
apex.env, and execute DML or PL/SQL via
The formal API documentation of these new APIs is still to come. They are however a subset of the Node.js Oracle Driver albeit with notable differences, such as the move from asynchronous to synchronous functions. You can also find examples in the respective attribute’s help text, and by navigating the code editor’s autocomplete.
In the past decade APEX developers went from manually escaping and concatenating strings, to using
apex_json, and nowadays to using the native JSON capabilities of the database via
Under the hood
We can of course assign anything to this namespace — functions, objects, variables, etc.
Snippets that follow, for example a Pre-Rendering Page Process, will have access to the same globalThis. Note that
variable1 cannot be accessed, as it was not assigned to globalThis.
This is different from the way PL/SQL processes work, where the only way to share data between them is by assigning it to page items or package variables.
At the end of the transaction, be that after page rendering, after an AJAX call process, or after page submission, any MLE context created by APEX will be purged.
In order to easily access these modules when we need them, we will need to expose a “require” function that is based on the module name, fetches the source out of the table, evaluates it, and returns the resulting object. We will name the function
requireModule so it will not conflict with the built-in
requirefunction. We can add this code to an Application Process that runs On Submit.
It’s also wise to add a server side condition to this process so it does not run on every submit, but only when we expect to run MLE code.
We can now use this infrastructure in an After Submit Page Process.
Let’s first put the
qrcodelibrary to test. If
P1_QR_DATA is a Text Field item, and
P1_QR_CODEis a Display Image Item, we can use this library to return the QR Code as a base64-encoded string.
On page submit, based on a URL entered in
P1_QR_DATA we can execute:
Which will result in the following:
Of course we could have used this library directly in the front-end like we’re already used to, but rendering the QR Code server-side allows us to achieve other things, such as embedding it in an email or report.
In this example we will use the
validate.js library to add a server-side validation for an email address.
Having a page item
P3_EMAIL and a validation on this item of type
Expression with value
requireModule('validator').isEmail(apex.env.P3_EMAIL) configured as follows:
helps us achieve the following behavior on page submit:
marked.js library can also come in handy if we store content as Markdown, but wish for it to be converted to HTML already on the server. Benefits of performing this conversion on the server include enhanced Search Engine Optimization, as well as the ability to embed the formatted content in emails.
To illustrate this, let’s convert the content of textarea
P5_MARKDOWN into HTML and assign it to a Display Only item
P5_HTML . The following code runs in an After Submit process: