Define h-OrderCalcs.p to calculate totals
- Last Updated: December 19, 2023
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
This section describes how to create a sample subprocedure, called
h-ordercalcs.p, to calculate the totals for the price and
extended price for all Order lines.
To create the first subprocedure:
- Open a New Procedure Window.
- Enter this ABL code:
/* h-OrderCalcs.p */ DEFINE INPUT PARAMETER piOrderNum AS INTEGER NO-UNDO. DEFINE OUTPUT PARAMETER pdOrderPrice AS DECIMAL NO-UNDO. DEFINE OUTPUT PARAMETER pdOrderTotal AS DECIMAL NO-UNDO. FIND Order WHERE Order.OrderNum = piOrderNum. /* Add up the total price and the extended price for all order lines. */ FOR EACH OrderLine OF Order: ASSIGN pdOrderTotal = pdOrderTotal + OrderLine.ExtendedPrice pdOrderPrice = pdOrderPrice + OrderLine.Price * OrderLine.Qty. END. - Save this code as h-OrderCalcs.p.
The h-CustOrderWin2.w test window will call this procedure. This
procedure has an INPUT parameter, which is an Order
number, and it passes back two OUTPUT parameters. It uses a naming
convention that begins each parameter with the letter p to help
identify them throughout the procedure.
The code finds the Order record for the Order number
passed in, and then in a FOR EACH block, it reads each
OrderLine for the Order and adds up two sets of
values. The first value, stored in pdOrderTotal, is the total of all
the ExtendedPrice values for the OrderLines. The
ExtendedPrice is the price after the Customer’s discount has been
factored in. The second value, pdOrderPrice, is simply the total of the
raw price for the OrderLine before the discount, which is the
Price field times the Qty (quantity) field.
After the FOR EACH block, the procedure ends and returns the output
parameters to the caller.
Notice that the FIND statement can be a unique find (without a qualifier
such as FIRST) because there can be only one Order
record with a given OrderNum value.
Next there is a new statement type in the FOR EACH block: the
ASSIGN statement. Any time you are assigning more than one value at
a time, as the code is here, it is more efficient to use this statement, which can
contain any number of assignments, each using the equal sign, and a single period at the
end.