Formula Statements

You use formula statements to provide instructions that you want your formula to carry out. When working with statements, it's important to have knowledge of the different statement types, the required order, and how to group statements.

Statement Types

Here are the statements that you can use in your formulas to provide instructions.

Statement

Statement Form

Description and Example

ALIAS

ALIAS name1 AS name2

Provides a different name for a database item or global value. Sometimes the database item names that the application provides are too long to use in a formula.

Use the ALIAS statement to shorten the name of a database item. Once the ALIAS is created, use it instead of the database item name. Using an alias is more efficient than assigning the database item to a local variable with a short name.

ALIAS OVERTIME_QUALIFYING_LENGTH_OF_SERVICE AS OT_QLS

ASSIGNMENT

variable = expression

array[index] = expression

Assigns an expression value to a variable or an array variable at an index position. A formula evaluates the expression on the right-hand side of the statement. It places its result in the variable you name on the left-hand side. The left side of an assignment statement must always be a local variable because a formula can only change the value of local variables.

Within a "CHANGE_CONTEXTS" statement, assign values only to contexts. Outside a "CHANGE_CONTEXTS" statement, assign values only to input, output, and local variables.

RATE = HOURLY_RATE + 14
WAGE = HOURS_WORKED * RATE

CHANGE_CONTEXTS

(context1 = expression1 [,context2 = expression2 ]

Changes one or more contexts within a formula. Within the "CHANGE_CONTEXTS" statement, use ASSIGNMENT statements to assign the new values.

CHANGE_CONTEXTS(AREA1 = TAX_REPORTING_UNIT_INCOME_TAX_JURISDICTION_GEOGRAPHY_ID)
(
  CHANGE_CONTEXTS(DEDUCTION_TYPE = 'SBJ_TO_REGULAR_TAX')
  (
    L_TAXATION_METHOD = 'NONE'
    EXECUTE('TAXABILITY_RULE_EXISTS')
    IF GET_OUTPUT('TR_EXISTS', 'N') = 'Y' THEN
      L_TAXATION_METHOD = 'REGULAR_TAX'
  ) /* DEDUCTION_TYPE context change undone here. */
) /* AREA1 context change undone here. */

DEFAULT

DEFAULT FOR variable IS literal

DEFAULT_DATA_VALUE FOR variable IS literal

The DEFAULT FOR statement provides a value that the formula uses for a formula input or database item in these situations:

  • The input wasn't assigned a value.

  • The database item's value couldn't be retrieved from the database.

  • A non-array database item's value is NULL.

The DEFAULT_DATA_VALUE FOR statement provides a value for an array database item where individual data values are NULL.

Some database items are defined to require a default value because they could return no data or NULL values from the database.

DEFAULT FOR HOURLY_RATE IS 3.00
INPUTS ARE HOURLY_RATE
X = HOURS_WORKED * HOURLY_RATE

EXIT

EXIT

Immediately exits from the enclosing WHILE loop. You can't use the EXIT statement outside of a WHILE loop.

FOUND = -1 /* -1 is not a valid index for A. */
I = A.FIRST(-1)
WHILE (A.EXISTS(I)) LOOP
(
  /* EXIT-clause for early exit. */
  IF A[I] = KEY THEN
  (
    FOUND = I
    /* Exit the loop. */
    EXIT;
  )
  I = A.NEXT(I,-1)
)

FORMULA CALLING FORMULA

SET_INPUT(input [,value]): Set an input or context value in the called formula.

EXECUTE(formula): Execute the called formula.

GET_OUTPUT(output, default-value): Get a value returned from the called formula.

IS_EXECUTABLE(formula): Test whether a formula is executable.

Calls a formula from another formula. For example, formulas can call a small formula that performs a common calculation. You can use this approach to avoid writing long formulas.

SET_INPUT('UNIT','Hourly')
EXECUTE('RATE_FORMULA')
HOURLY_RATE = GET_OUTPUT('RATE',0.0)
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN WAGE

IF

IF condition THEN statements

IF condition THEN statements ELSE statements

Executes one or more statements if a condition is true. Use the IF ELSE statement to specify a set of statements to execute if the condition is false.

IF (AGE < 20) THEN
  TRAINING_ALLOWANCE = 30
ELSETRAINING_ALLOWANCE = 40

INPUT

INPUTS ARE input1 [,input2]

Lists the input variables for the formula. There's only one INPUT statement in a formula .

INPUTS ARE HOURS_WORKED
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN WAGE

RETURN

RETURN [ output1 ] [,output2]

Causes a formula to stop executing immediately. For its value to be returned to the caller, you must enter a formula output variable in the RETURN statement that stopped the formula.

You can enter multiple return statements in a formula.

INPUTS ARE HOURS_WORKED
IF HOURS_WORKED <= 10 THEN(
  RETURN
  /* This is ignored. */
  BONUS = 10
)
/* This is executed if HOURS_WORKED > 10. */
BONUS = 50
RETURN BONUS

WHILE

WHILE condition LOOP statements

Executes a number of statements as long as one condition is true.

If the WHILE statement loop performs too many iterations, an error occurs to prevent endless looping.

/* -1234 is not a valid index for A in this instance, so use as default. */
NI = A.FIRST(-1234)
WHILE A.EXISTS(NI) LOOP
  VA = A[NI] /* Do some processing with element at index NI. */
  NI = A.NEXT(NI,-1234) /
 Go to next index. */
)

WORKING STORAGE

WSA_DELETE([item]) - Deletes values from the storage area.

WSA_EXISTS(item[,type]) - Determine if an item exists .

WSA_GET(item, value) - Fetches values from the storage area.

WSA_SET(item, value) - Sets values from the storage area.

Stores reference data, which you can set, fetch, or delete.

/* Formula: RATE_SETTER */
WSA_SET('RATE:HOURLY1',3.5)
WSA_SET('RATE:HOURLY2',4.0)
WSA_SET('RATE:HOURLY3',4.5)
WSA_SET('RATE_FLAG','Y') /* Flag to say that the rates have been set. */

Ordering Statements

Place the statements in this order in the formulas:

  1. ALIAS statements, if any

  2. DEFAULT statements, if any

  3. INPUT statements, if any

  4. Other statements

Grouping Statements

If you want to group more than one statement under IF/THEN statements, ELSE clauses, WHILE loops, or CHANGE_CONTEXTS, enclose the group of statements within brackets. In the absence of brackets, the preceding statement applies only to the first statement.

Here's an example of how you can group statements:

I = A.FIRST
WHILE (A.EXISTS(I)) LOOP
(
  A[I] = I
  I = A.NEXT(I,-1)
)

Here's an example of how you should not group statements::

I = A.FIRST
WHILE (A.EXISTS(I)) LOOP
  A[I] = I
  I = A.NEXT(I,-1) /* This is not executed as part of the loop. */