Introduction
Many fields can be configured with scripts and simple expressions however for the purposes of the examples here we have assumed that the reader is configuring a Stream Attribute field unless otherwise stated. This field is in no way special and in general all of the scripting elements and techniques described are available in all of the dynamic fields. Any special cases are documented on the help screen describing the individual form.
Assigning a Literal Value to a Field
The most basic expressions simply assign a constant value to the field. For example these three expressions assign the integer 3, the floating point number 3.2 and the string "Hello" to the field:
Expression | Result |
---|---|
3 | 3 |
3.2 | 3.2 |
"Hello" | "Hello" |
Panel | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
|
Insert excerpt | ||||||||
---|---|---|---|---|---|---|---|---|
|
$-variables are not really needed in simple calculations. They are useful:
- in forEach loops
- when using look-up pipes
- to store a value from a look-up pipe, when you need the value in multiple attributes.
- In the first attribute, look-up the value.
- Use a $-variable to store the value that is returned by the look-up.
- When you need the value in another attribute, reference the $-variable instead of repeating the look-up.
- in complex expressions, to break up multiple consecutive functions into separate lines. In this case, use a $-variable to store the intermediate values; see Multiline Statements and $-variables, below.
Assigning Values From a Pipe
One of the most common type of expression isIn stream attribute expressions that have complex calculations, you may need a variable to hold the result of a calculation. You can then reference the variable later in the expression, or in other attribute expressions. In PhixFlow you can use a $-variable for this, by prefixing the variable name with the $ symbol.
Tip |
---|
It can be helpful to distinguish between $-variables that are:
|
title | Simple $-variable example |
---|
The following expression sets 2 $-variables $A
and$ B
, then uses them in a simple calculation that returns 10.
Code Block |
---|
do(
$A = 5,
$B = 2,
$A*$B
) |
Another expression can reference the $A and $B variables in a different calculation. The following expressin returns 100.
Code Block |
---|
do(
$c = 10,
$A*$B*$C
) |
Adding Comments
In longer scripts (and even short scripts) it is a good idea to add comments to remind yourself (and anyone else who may need to modify the script in future) what steps are being followed. For short, 1 line comments, just prefix the line with // and for longer, multiline comments, enclose the lines in /* .... */. For example:
/* Get the discount rate that should apply to the calls which match the filter conditions */ $discountRate = discount.rate, // Now apply the discount $value = $value * ( 1 - $discountRate)
The lines above are comments and are ignored by PhixFlow.