Thursday, February 25, 2010

Branches with Conditions When Button Pressed - be careful

Consider this example; we have a typical APEX Report with Form.

When we click on the Create button we come into the Form and normally you would see just one Create button. If you hit that button you get back into the Report.
In some projects I use a "Create and Create Another" button as people need to enter more records at a time and this way they stay in the Form when they hit that button.

So how do you create such a button? I typically do:
- copy the Create button
- add a Branch that has as Target Page the same page and add a condition to fire on that "Create and Create Another" button. Be sure to put that Branch (sequence) before the unconditional branch.

If you test that Form and you hit on the "Create and Create Another button" you stay on that page. Works great... so you think you are done, however if you hit the Create button you want to go back to the report (like it was before). But this doesn't work anymore, it stays on the same page too. So for some reason, although the Branch has a condition to fire on the "Create and Create Another" button, it ALSO fires on the normal Create button.

After having a closer look at what APEX is doing, it turns out that APEX is using the Request value or incase of a button, the button name. As the buttons have the same name (the condition of) the Branch fires for both buttons.

If you use the standard "Automatic Row Processing (DML)" to handle the insert/update/delete it requires the Request value to be a specific one. In case of Insert you have following options: INSERT, CREATE, CREATE_AGAIN, CREATEAGAIN

So the solution to the above problem is to rename your "Create and Create Another" button to e.g. CREATE_AGAIN

I found it confusing as the select box let you think it's a condition on a button. The behaviour is the same in APEX 3.x and 4.0.

I would suggest to the APEX team to only show the request values or make it so the branch really knows which button got clicked.

No comments: