Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

This page explains the basics of writing expressions and scripts in PhixFlow. The instructions assume you are configuring a Stream Attribute,  unless otherwise stated.

Tip

When you are writing and an expression or script, always check the relevant help page. This will have information about any restictions or special cases that apply.

Assigning a Literal Value to a Field

The most basic expressions expression simply assign assigns 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:

ExpressionAssigns value
Result
Data type
33integer
3.23.2floating point
"Hello""Hello"
Note that if you want to assign a
string
value (as in the last example) then the characters must be enclosed in either single 'Hello' or double "Hello" quotes.



Panel
borderColor#7da054
titleColorwhite
titleBGColor#7da054
borderStylesolid
titleSections on this page

Table of Contents
indent12px
stylenone



Tip

To assign a string, it must be enclosed in:

  • either single quotes: 'Hello'
  • or double quotes: "Hello"


Parameter Types 
Anchor
paramTypes
paramTypes

When writing expressions, it is important to consider the type of the result. For example, if you have written a Stream Attribute you can write an expression to return the integer 3 but have defined the type of the Stream Attribute stream attribute to be a String then PhixFlow will stop and report the error a string. If there is a mis-match between data types, when the expression is evaluated, PhixFlow stops evaluating the expression and reports an error.

The full list of types handled by PhixFlow is shown below. It's not necessary to understand the meaning of the individual types at this stage, however just be aware that the different types exist.in the following table. 

TypeDescription
ArrayA list values, e.g. [1,2,3]. For details about what other values arrays can hold and Array handling in general see Arrays and Record Sets
.
DateA date. For details about date manipulation functions go to Date Functions
.
DatetimeA date and time. For details about date manipulation functions go to Date Functions
.
FloatA floating point number, e.g. 3.2 or 3.0 or 0.0
IntegerAn integer, e.g. 3 or 0
NumberAn integer or floating point number
StringA string value, e.g. "Hello" or the empty string ""
BooleanA logical value of either true or false.
Note that any

Any non-null, non-zero value is treated as true
Regular ExpressionRegular Expressions allow string patterns to be expressed
. See Regexp ToDelete for more details
; see Regular Expressions.

Using $-variables in Calculations
Anchor
dollarvar-intro
dollarvar-intro

In 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 for the same stream. 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:

  • only used in the current attribute: use a lower case variable name, for example $percent
  • also used in another attribute: use an initial capital letter, for example $Percent.


Panel
titleSimple $-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 Anoth;er expression can reference the $A and $B variables in a different calculation. The following expressin expression returns 100.

Code Block
do(
   $c = 10,


   $A*$B*$C
)


$-variables are not really needed in simple calculations. They are useful:

  • in forEach loops
  • when using look-up pipesCalculating attibute names from pipes; see Assigning Values From a Pipe, below.
  • to store a value from a look-up pipe, when you need the value in multiple attributes.
    1. In the first attribute, look-up the value.
    2. Use a $-variable to store the value that is returned by the look-up.
    3. 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

PipeOne of the most common type of expression is

Pipe 
Anchor
pipe
pipe

Stream attribute expressions are commonly used to fetch the value returned by a Pipe. This is simply done by suffixing the Pipe pipe name with the name of the attribute to be returned. For example: 

  in.accountRef

This expression causes PhixFlow to look at the pipe named "in" and return the value in the field called "accountRef".

Tip

This expression can return multiple values; see Arrays and Record Sets.


Calculating an Attribute Name

If the name of the attribute whose value you want has to be calculated based on other factors

then

, you can assign the attribute name

can be assigned

to a

variable (see the section below on mulitline statements and $-variables) and the variable used in place

$-variable. You then use the variable instead of the attribute name itself

e

.

g.

For example:

  in.$attrName

If the name of the attribute whose value you want contains characters that would otherwise be invalid you can either

Handling Invalid Characters

There are two options for handling an attribute name that contains invalid characters.

  • Either assign that name to a variable
(see the section below on multiline statements and $-variables)
  • and use the variable instead of the actual attribute
as discussed above or simply
  • .
  • Or enclose the name in quotes
e.g.
  • , for example:  in.'account-ref'.

Mathematical Operators (+, -, *, /)

Several values can be combined using the usual basic mathematical operators. For example when in.numProducts has the value 9:

ExpressionResult
in.numProducts + 312
in.numProducts - 36
in.numProducts * 327
in.numProducts / 33

The usual calculation precedences apply i.e. , so * and / operations are carried out before + and -. When the mathematical statements involve multiple calculations, use brackets ( ) can be used to force the correct processing order:.

ExpressionResult
3 * 3 + 312
3 * (3 + 3)18

String Concatenation (+)

The + operator also serves as the string concatenation operator i.e. joins . This means you can use + to  join strings together. For example if in.customerName returns "Smith" and in.title returns "Mr":

ExpressionResult
"Hello" + " to " + "you""Hello to you"
"Dear " + in.title+ " " in.customerName + "," +"Dear Mr Smith,"


Note

If you use + to concatenate a String string and a numeric value, such as an Integernumber (integer or floating point), then PhixFlow treats the number as if it is a string. For example:

ExpressionResult
"Hello" + 3"Hello3"
"4" + 3"43"


Multiline Statements and $-variables
Anchor
dollarVar
dollarVar

Often there is a need to work out the result of a When you are working with more complex expression which , you may require have several different statements to be evaluated evaluate first. To group several statements together in this way, you should make use of the do() function. In nearly all multiline statements, it is also very convenient

In multi-line statements, you usually need to assign the value of an expression to a $-variable which may then be used , to use later in the expression. For example:

ExpressionResult
do ( $name = in.customerName, $title = in.title, $salutation = "Dear", $result = $salutation + ' ' + $title + $name )"Dear Mr Smith"


Note

Every statement within Statements in the do() function (except the last) ends end in a comma "," except the last one.

The value of an expression , that is the value that will be assigned into the stream attributeused, is the value of the last statement to be evaluated. In simple expressions, such as the example above example, the last statement to be evaluated is the last line. However, in Conditional Statements, the last statement evaluated is not always the last statement in the expression. For example:

Responsive

ExpressionResult
if ( in.customerName == "Smith", 45 , // else 36 )45 if the customer's name is "Smith" but otherwise the value 36.

Adding Comments

It is always a good idea to add comments to your scripts to explain its steps. These Comments help anyone who needs to modify the script in future.

There are two ways to tell PhixFlow that a line is a comment, and can be ignored.

NotationUse for
//

 short comments up to 1 line

/* <comment> */multi-line comments

For example:

/* Get the discount rate that should apply to 
the calls that match the filter conditions */
$discountRate = discount.rate,

//
Now apply the discount
$value = $value * ( 1 - $discountRate)


See Also