Tuesday, March 02, 2010

Trying Out Apex 4.0 Dynamic Actions

In my spare moments I am currently familiarising myself with the Apex 4.0 Early Adopter edition. One of the many exciting new features is Dynamic Actions. These allow you to add functionality to your pages that would previously have required writing Javascript, AJAX calls and On Demand PL/SQL processes, but can now be done declaratively.

The following very simple example shows 3 uses:
1) Enabling/disabling one field according to the value of another
2) Calculating and displaying a value when items are updated
3) Retrieving information from the database when an item is changed.

There is little documentation about Dynamic Actions yet, so I'm not sure I have always taken the best approach.

My example is a "Create Employee" page that inserts a row into the familiar EMP table:

I have created a Dynamic Action that makes the Commission item enabled only when the selected job is one of 'MANAGER' or 'SALESMAN'.

MANAGER selected:

CLERK selected:

These are "simple" actions, I merely had to specify which item values trigger the action like this:

... and then specify the item to be enabled/disabled.

Another dynamic action computes the "Total Package" as SAL+COMM when either of these two items are updated:

I did this using a Javascript fragment rather than PL/SQL, to avoid an unnecessary AJAX trip to the database:

(OK so I did have to write some Javascript! But not much.)

A third dynamic action fires when a manager is selected; this looks up the selected employee and gets his/her job title and department name and dislays them in 2 page items:

This last action isn't as efficient as I'd like because it results in 2 AJAX calls: one to get the job and one to get the department name. This could be a case of me just not knowing the right way to do it in one hit. I used 2 "true actions" that use SQL to set the value of an item. The first has this SQL:
select job from emp where empno = :p2_mgr

and the second:
select dname
from dept join emp using (deptno)
where empno = :p2_mgr


Anthony Rayner said...

Hi Tony,

Nice post. I just wanted to comment on your final example and point you to a recent thread on the OTN forums that discussed exactly the point you raised.



yavuz said...

Hi Tony,

I would like to ask a problem.
I have categories and parent categories in a table. This category dept can be 3 or 4. I have 4 select lists and these select lists have cascading LOV to fill sub-category. Here i would like to enable or disable sub select list depending on if it has options or not. I tried many ways but i couldnt do it.
Can you make an example like that.?

Thank you very much

Tony Andrews said...

Yavuz, see this demo I just set up, is that what you mean? http://apex.oracle.com/pls/otn/f?p=35662:9 If so, the trick is to create an item to hold the count of rows returned by the second LOV (it would be hidden but I left it displayed for testing purposes). Then create 2 dynamic actions: an action on the first select list to populate the count, and an action on the count to disable the second selec list when the count = 0.

Tony Andrews said...

... however I have just found that while it works in Firefox it doesn't work properly in IE! Oh well...