Monday, 14 October 2013

APEX 101 - Server-side Dynamic Actions

Dynamic actions are awesome.

Let's so you have a scenario where after you change the value of one item, you would like to validate it or perhaps calculate the value of a separate item - or both!

Given an entry of Radius, I would like to calcular Sphere area.
Let's add an Oracle Forms style when-validate-item
This requires a relatively simple Dynamic Action that fires on 'Change' of the field P16_RADIUS.
In this case no condition is required - any change will fire the action. You could ensure the value is above zero, for example.
Dynamic Action properties
The action itself contains the PL/SQL formula.
:P16_SPHERE_AREA := POWER(:P16_RADIUS,2) * 4 * 3.141592;
This can be any anonymous block, or call to a PL/SQL package.
'True' action properties
The most important part of this action are the 'Page items to Submit' and 'Page items to return' fields.
This is what passes information from the browser to the database (session state) and back again.

In this case, the P16_RADIUS value is sent from the browser to the database so it can be used in the PL/SQL expression calculation, then the resulting P16_SURFACE_AREA is set in the browser.

Note some of the other options facilitated by the checkboxes.

The flexibility that dynamic actions provide give me just cause to vote them the best feature of APEX 4.x, just above plug-ins.

Other links

Inspired by this forum post, amongst many others like it.
For a more in depth tutorial, check out this Oracle-by-example.
This example is among others on session state in a page of my sample application.

No comments: