Tuesday, December 13, 2011

APEX Process Condition with Multiple Buttons

For new APEX developers, adding a page process condition can be a bit confusing at first when basing it on multiple buttons. This post will go through a scenario on how to easily use multiple buttons in a page condition.

The Setup
A classic example of having multiple buttons on a page is when you want to save or update a record. The Save button should only appear for new records, whereas the Update button should only appear for existing records. The catch is that when saving and updating the page you may need to run the same process.

The following setup highlights this classic example. Create a region in a page with two fields: P1_X and P1_Y. Then create three buttons: Save, Update, and Cancel. All three buttons should submit the page and an example of the page is shown below


Create the following page process:
Name: Update P1_Y
Process Point: On Submit - After Computations and Validations
Process: :P1_Y := :P1_X * 2;

The Problem
You'd like this process to run when both the Save and Update buttons are clicked. If you scroll down to the process's conditions area you'll notice that it has an option to restrict the process when a particular button is clicked (shown below).


As you can see you can only select one button from the list. At first glance the only option that you have to resolve the two button issue is to create a new process, which is a copy of the existing process, and set the condition to the other button. That doesn't necessarily make sense and can be an obvious maintenance nightmear.

The good news is that there is a very simple way to get around this issue. When you click a button it actually sets the REQUEST variable to the button name. The button name is defined in the button. For example the Save button's request is actually SAVE (shown below).


Going back to the original problem, and leveraging what we know about the REQUEST variable, you can modify the process's condition to have it run when either the Save or Update buttons are selected as shown below.


Note: They're other ways to use the REQUEST variable in the conditions section. This one highlights a very simple use.

7 comments:

  1. I really should look more closely at the available "conditions". Before reading this, I would of just made a process based on each button.

    ReplyDelete
  2. Hi Jon,

    I'm glad that you found this helpful.

    Martin

    ReplyDelete
  3. Nice post Martin!

    You could also make it a conditional function body returning boolean and use :REQUEST bind variable:


    begin
    if :REQUEST in ('SAVE','UPDATE') then
    return true;
    else
    return false;
    end;

    ReplyDelete
  4. @Alex idea is also good if we have complex condition eg. the value of a button should have some value

    begin
    if :REQUEST in ('SAVE','UPDATE') and :P1_itemname = 'A' then
    return true;
    else
    return false;
    end if;
    end;

    ReplyDelete
  5. Thanks Giffy,

    I have a page with 5 different forms and their submit buttons, just used your suggestion and everything works very well.

    Thanks keep it up

    ReplyDelete
  6. Hi Martin,

    thanks for this simple solution, which is already built in in APEX!

    ReplyDelete
  7. Hi Martin,

    Brilliant. Thank you.

    ReplyDelete