Overview of Using Fast Formula Components
When you're developing a fast formula, you must understand the formula language, the rules that the application imposes on the fast formula, and the calculation requirements.
Create fast formulas using these components:
-
Assignment statements
-
Return statements
-
Variables
-
Input statements
-
Expressions
-
Conditions
-
Comments
Let's look at an example to understand how each component is used in a fast formula. Suppose you want to calculate the pay value for the WAGEelement by multiplying the number of hours an employee works each week by the hourly rate. Here's how you can write the formula in this example:
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN WAGE
Assignment Statements
An assignment statement assigns a value to the WAGE
element.
Return Statements
A return statement passes the WAGE
value back to the payroll run. You can use a return statement to
stop the formula execution without passing any values.
Variables
Variables are of these classes:
-
Input variables appear in INPUTS statements and bring values into a fast formula.
-
Output variables appear in RETURN statements and return values from a fast formula. A variable can be both an input and output.
-
Local variables are only used within one formula.
You can change a local variable within the formula
by assigning a value to it using an assignment statement. To calculate
the WAGE
value, the fast formula needs
to get the value for the HOURS_WORKED
variable.
You can use local variables to store data in a fast
formula. You might want to hold data temporarily while you perform
some other calculations, or pass data back to the application. Here's
an example of the ANNUAL_LEAVE
variable.
/* Formula: Annual Leave Formula */
IF YEARS_SERVICE >= 10
THEN
ANNUAL_LEAVE = 25
ELSE
ANNUAL_LEAVE = 20 + FLOOR (YEARS_SERVICE/2)
RETURN ANNUAL_LEAVE
Input Statements
You can use HOURS_WORKED
as an input value of the WAGE
element.
To pass the element input values to the fast formula during processing,
define an input statement like this:
INPUTS ARE HOURS_WORKED
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN WAGE
HOURS_WORKED
input variable is numeric. If
the input variable isn't numeric, you must specify the type. For example, Expressions
Each function or calculation is one expression. You can nest expressions to create more complex calculations. You can use brackets to control the order in which calculations are done.
The formula evaluates expressions within the brackets first. Within nested brackets, evaluation proceeds from the least inclusive set to the most inclusive set. If you don't use brackets, the formula evaluates expression in this order:
-
Multiplication, Division
-
Addition, Subtraction
Expressions combine constants and variables with
operators (+, -, *, /), array methods, and functions to return a value
of a certain data type. For example, the expression (3 + 2) returns
a value of 5, and is a NUMBER
data type.
The format of an expression is:
SUBEXPRESSION [operator SUBEXPRESSION ...]
You can combine a number of sub-expressions into a
single expression. For example, you can combine the sub-expressions
(3 + 2) and MONTHS_BETWEEN
(start_date,
end_date) into a single expression as follows:
(3 + 2) + MONTHS_BETWEEN(start_date, end_date)
You can also use expressions inside functions, such as:
salary = GREATEST(minimum_wage, (hourly_rate * hours_worked))
Operands in an expression are usually of the same data type, which is the data type of the expression as a whole. Here's an example of an expression in which all the operands are numeric and the expression itself is numeric:
GREATEST(MINIMUM_WAGE, (HOURLY_RATE * HOURS_WORKED)) + BONUS
BONUS
is the operand
for the above expression. The return value is GREATEST
. The arguments for GREATEST
are separate expressions.
Conditions
You can use conditions to process expressions based on whether a certain condition occurs. For example:
TRAINING_ALLOWANCE = 0
IF (AGE < 20) THEN
TRAINING_ALLOWANCE = 30
This formula checks if the condition (AGE < 20)
is true or false. If it's true, the formula processes the statement
that follows the word THEN
. If the
condition is false, the formula ignores this statement.
Comments
Use comments to explain all or part of a fast formula. Also, you can change some formula lines into comments until they're ready to be used. You can place comments anywhere within a formula. The beginning of a fast formula should contain these comments:
-
The formula title and a short purpose statement.
-
A description of the formula inputs.
-
A list of variables and literals that may require updating.
-
An explanation of the formula's calculation.
-
The dates of any modifications, the name of the person modifying the formula, and the reason for the change.
Multi-Line Comments
Multi-line comments are designated by the comment delimiters of /* and */. Anything written inside these delimiters is a comment.
Single Line Comments
Fast formula also supports single line comments. The # character is used for the start of single line comments. The # character itself and any text after it to the end of the line are ignored.
Comments Example:
# This line is a single line comment and will be ignored.
/*
* This is a multi-line comment.
*/
a = 1 # Ignore the # character and everything after it on the line.
b = '####' # b is the string ####. The remainder of the line is ignored.
return a, b