Formula Extension (FOX)
Use
You use the SEM-BPS formula extension FOX within the formula function, in order to access transaction data of the planning package more flexibly than when you sequentially, uniformly process all data records with the help of available operators. In addition to the formula operators, the formula extension offers you the possibility to make statements dependent on conditions, to run through loops, and issue messages.
Formula extensions contain a few, easy to learn language elements, with which you can solve almost all planning tasks, which occur in practice. The syntax of this language element is very similar to the SAP programming language ABAP. Therefore, you can use any existing ABAP knowledge, without having to concern yourself with the complexity of a full programming language (for example, memory management, database access).
Integration
The formula extensions are not an independent SEM-BPS function type, they are an enhancement to the formulas. You can continue to create formulas in the usual way, without having to make use of the formula extensions.
Features
General
All formula extension statements must be completed with a period "." A statement can extend over more than one line in the formula editor, and several statements can be in the same line. No distinction is made between upper and lower case. The system automatically converts lower case entries into upper case entries.
Comments
You can create comments in the formula editor to explain the statements you programmed, and to make the code easier to read. To do this, set the indicator
* in the first column of a row. This means that the row is ignored by the syntax check, and is skipped when executing the function.
It is important that you write the indicator
Syntax
* comment text
Explanations
comment text:
Any character string, which is not interpreted by SEM-BPS as a statement. The character string cannot extend over several lines.
Data Declaration (Local Variables)
You can declare local variables for a parameter group in the formula editor. You use these variables for example to check the dynamic conditions at the runtime of the planning function, which are dependent on the current key figure values. First you could determine the largest or smallest posted value of a key figure within a loop using the transaction data. Then you can save this value in a variable and then generate new plan data independently of the variable value. The system initializes variables of the types
F and I with the value 0.
For the language elements
Syntax
DATA varname TYPE typname.
Explanations
varname:
Name of the variable. The name can contain letters and figures, but must always begin with a letter. The name must differ from the reserved words of the formula extension. All language elements belong to this (for example
typname:
Name of the data type for the data saved in the variables. The permitted types are divided into two classes: predefined types and context-specific types.
Predefined types:
I
: Integer. Used to save whole number values. For example, you use this type for the declaration of control variables with which you can count the number of iterations in a loop construct.Context-specific types:
Context-specific types are types, which refer to the key figures and characteristic in the planning level to which the formula function belongs. Which types are allowed depends on which characteristics of the planning level were included in the list of fields to be changed of the formula function. The correct type name is then identical with the technical name of a characteristic. A variable whose type refers to a characteristic of the planning level, can accept all values which are maintained for this characteristic.
The
Example
In the following example, the system loops through all key figures of the planning level using the
KeyFigure variable. Key figures, which contain the value 0, are assigned the value 1. The other key figures remain unchanged.DATA KeyFigure TYPE KEYFIGURE_NAME.
FOREACH KeyFigure.
Conditional Statement
Here you determine that a statement block is only executed if the specified condition is fulfilled. The conditional statement is always introduced by the language element
IF and closed with ENDIF. You can include as many alternative conditions in a conditional statement as you like with the language element ELSEIF. As soon as one of the specified conditions is correct, the statement block belonging to it is executed, and then the statement is exited. With the language element ELSE, you can plan one statement block, which is then executed if none of the conditions specified before apply.Syntax
IF
expression1.Explanations
expressionn:
Logical expression. If expressionn results in the value
statementn:
Statement block, which is then executed, when the check of the previous logical statement results in the value
Example
In the following example sales per customer are planned. The size of the planned sales increase depends on the size of previously made sales: The sales with customers, to which a sales revenue of more than 50000 € is already allotted, should increase by 10%, while the increase with customers under this threshold should amount to 20%. The formula operand used in the example has the form { key figure name, customer, version }. Version 0 contains the actual data, version 1 the plan data.
DATA Customer TYPE 0CUSTOMER.
FOREACH Customer.
Loop Constructs
The formula extensions offer two different language elements for the programming of loops:
DO ... ENDDO and FOREACH ... ENDFOR. Both language elements are used for different purposes:Syntax
1.
DO ... ENDDODO.
2.
FOREACH ... ENDFORFOREACH
varname [, varname].Explanations
expression1:
Logical expression, which defines the termination criterion for loop processing. When expression1 results in the value
statement1:
Statement block, which is executed in every loop pass.
statement2:
Optional statement block, which is executed when the termination condition is reached.
varname:
Variable, which refers to a characteristic contained in the planning level, or to the quantity of key figures contained in the planning level (type

With the
Example
1.
DO ... ENDDOThe example shows how you can calculate the factorial of a number (here the factorial of 5) with the statement
DO ... ENDDO. ENDDO the factorial of a number (here the factorial of 5) can be calculated. It is very important that the loop passes are counted in the variable Counter, and the loop is exited with the EXIT statement when the termination criterion is reached.DATA Factorial TYPE I.
DATA Counter TYPE I.
DATA Result TYPE I.
Result = 1.
Counter = 0.
Factorial = 5.
DO.
2.
FOREACH ... ENDFORIn the following example, the system loops through all key figures of the planning level. Key figures, which contain the value 0, are assigned the value 1. The other key figures remain unchanged.
DATA KeyFigure TYPE KEYFIGURE_NAME.
FOREACH KeyFigure.
Messages
You can issue messages to point out errors and special features to users when executing a formula function, or to give information about processing progress with time-consuming operations.
Syntax
MESSAGE
Tnnn (class) [WITH var1 [[[var2] var3] var4]].Explanations
T:
Type of message. Allowed values are

If you issue a message of type
nnn:
Three-figure number of the message to be issued within the message class.
class:
Entry of the message class enclosed in round brackets, to which the message to be issued belongs.

As far as possible, use messages from a message class you created in the ABAP Workbench. In this way, you make sure that your messages are independent of possible changes to delivered message classes. However, you must transport the message class you created, from the test system into the production system.
WITH
var1 [[[var2] var3] var4]:Optional extra, to issue up to four text variables in the message text. You can use local variables, which reference characteristics or the list of key figures, as text variables, or text constants. The text variables are inserted into the static message text (short and long text) in the places, which were prepared for it with the placeholder
Example
In this example, the planned price of an article is increased by 10% for all customers. If the price for a customer is with the value 0 in the data basis, then an error message is issued, which also contains the name of the customer. Because of the system behavior described above with messages of the type E, no key figure values at all are changed in this case (also not for data records in which the price has a value different to 0). To execute the price increase, you must first change the data records with the value 0 or remove it from the selection.
The formula operand used in the example has the form { key figure name, customer, version }. Version 0 contains the actual data, version 1 the plan data.
DATA Customer TYPE 0CUSTOMER.
FOREACH Customer.
Special Data Access Functions
Access to Time Characteristics
You can access time characteristics with the function
TMVL. The function returns the current value of the specified time characteristic, if necessary, marked up with an offset, which is also specified. The entry of an offset offers the advantage that in this way such characteristics can also be delivered, which are not contained in the transaction data of the current planning package.Syntax
TMVL(
characteristic name, offset)Explanations
characteristic name:
Technical name of the time characteristic, whose value should be calculated.
offset:
Whole number offset, with which the current value of the time characteristic is marked up. The entry of the offset is obligatory. If you want to transfer the value of the time characteristic unchanged, then enter an offset of 0.
Example
In the following example, the net book values of an asset are calculated with straight-line depreciation over a five year duration of depreciation. Cost price is 1000, net book value 100, depreciation percentage rate 20%. The function
TMVL determines the value of the time characteristic FYEAR and adds an offset of 1. The value increased by 1, is assigned to FYEAR within the DO...ENDDO loop, so that the calculation of the net book value is executed for a sequence of five fiscal years. After that the iteration is terminated. The operand for this formula has the form { key figure name, fiscal year }.DATA YEAR TYPE I.
DATA FYEAR TYPE 0FISCYEAR.
FYEAR = VARV('ACTYEAR').
DO:
Access to Attributes
With this function, you can access data, which neither concerns characteristics or key figures, but master data attributes, which can be assigned to a characteristic.

The attribute value of a characteristic that is compounded to a superordinate characteristic, can only be determined with the ATRV function, if the superordinate characteristic is not included in the list of characteristics to be changed.
Syntax
ATRV(
attributname, varname)Explanations
attributname:
Technical name of the attribute, whose value should be calculated.
varname:
Variable, which refers to the characteristic, to which the attribute belongs.
Example
In this example, the prices are conducted as attributes for the characteristic article. You use the
ATRV function to determine the attribute value. The function expects the technical name of the attribute to be read (for example 0PRICE) as parameter, and a variable, which refers to the characteristic, to which the attribute belongs (for example 0ARTICLE). The operand for this formula has the form { key figure name, article }.DATA ARTICLE TYPE 0ARTICLE.
FOREACH ARTICLE.
Access to Variables
With this function, you can determine the value of a (global)
Variable, which was defined in the context of the current planning area. 
In the context of formula extensions, only such variables can be used, which are restricted to single value. Otherwise the system cannot clearly determine the value of the variables.
Direct access to a global variable using its name (in a value assignment with the form "{Operator} = varname.") is not possible. Always use the
Syntax
VARV(
varname)Explanations
varname:
Technical name of the variable.
Example
In the following example two variables of the type 0CUSTOMER are declared. One of them is used as a control variable for an iteration over all customers. The other one is reserved using the
VARV function with the value of the global variable VCUST, as it was defined in the context of the planning area. In the iteration, as soon as the customer defined by the variable is reached, the planned revenue for all articles is increased by 10%. Subsequently, the iteration is terminated with the EXIT statement. The operand for this formula has the form { key figure name, article }.DATA ARTICLE TYPE 0ARTICLE.
DATA CUSTOMER TYPE 0CUSTOMER.
DATA VCUSTOMER TYPE 0CUSTOMER.
VCUSTOMER = VARV('VCUST').
FOREACH CUSTOMER.
Determination of the Current Characteristic Value
With this function, you can transfer the current value of a characteristic to a variable, and in this way find out which data was currently edited. The function
OBJV is called up without entering parameters. First the type of the variable (and with it the characteristic, to which the variable refers) is determined, to which the function result should be assigned. Then the function determines the current value of the characteristic referenced by the variable, and returns the value as function result.Above all, the function is used to keep the list of fields to be changed as small as possible. This has the following advantages:

The result of the function
Syntax
varname
= OBJV()Explanations
varname:
Technical name of the variable.
Example
In the following example, the example used above for the
VARV function is enhanced in the following way: An additional variable is declared of the type 0COUNTRY, to which the current value of the characteristic is transferred within the FOREACH loop. Revenue planning is executed as in the example above, however, not for articles that were sold in Germany. Prerequisite for calling up the OBJV() function is that the characteristic 0COUNTRY is not contained in the quantity of fields to be changed.DATA ARTICLE TYPE 0ARTICLE.
DATA COUNTRY TYPE 0COUNTRY.
DATA CUSTOMER TYPE 0CUSTOMER.
DATA VCUSTOMER TYPE 0CUSTOMER.
VCUSTOMER = VARV('VCUST').
FOREACH CUSTOMER.
See also:
Formula Extension: Examples