Creating a computation
Computations are events that will prepare items with data. As the name implies, computations can "compute" how data is to be shown on screen or how data is handled after submitting. Computations can be triggered during page rendering, but they can also be used in the After Submit process.
This example will show an implementation of a computation, but there are many more possible uses for computations.
How to do it...
Start by creating a normal text item on the form that was created in the second recipe of the first chapter. This is a normal form based on the EMP
table. Normally, a user would have to manually enter the commission. What we are going to do is to create a computation that will automatically enter an amount into this field, based on a percentage of the salary and a bonus for people that work in the Sales department.
First, identify the item that holds the commission column.
The next step is to find the moment when we want to execute the computation. In this case, before the page is submitted but after the Save button is pressed. So in this case we will create the computation After Submit.
Right-click on the Computations under After Submit in the Page Processing component and click on Create:
Choose Item on this page and press Next.
Select P5_COMM from the Compute Item select list, select PL/SQL Function Body from the Computation Type select list and click on Next.
The next step is to enter the PL/SQL code for this computation:
declare l_comm number; begin if :P5_DEPTNO = 30 then l_comm := :P5_SAL * 1.10; else l_comm := :P5_SAL * 1.05; end if; return l_comm; end; [9672_01_15.txt]
Click on Create.
The computation is now done. It can be tested by selecting an existing record from the list of employees and altering its salary and then saving the data. Another option is to create a completely new employee. You will see that an employee under the Sales department with dept no. 30 will receive a 10 percent commission whilst employees from all other departments will receive 5 percent.
There's more...
Computations can be used for far more situations than preparing an item before saving it to the database.
An example of another implementation can be an item that will show the current date when the page loads, or an item that will show a total amount when certain other amount items are entered. Used with dynamic actions, computations can be very powerful.