Scripting Language for Additional Events #
The syntax and functions available in the scripting language are described in the corresponding section of this manual. Among other things, this resource allows you to program the execution of different types of events in a page funnel step. Before starting, it is recommended that you review the following sections: Variables, Comments, Arithmetic operators, Comparison operators, Function declarations, Control structures, Data structures, Mathematical functions, Text functions and Other functions.
Code Editor #
To access the code editor, you need to edit the page funnel step where you want to execute additional events. To begin, enter the Layout Settings section:
Then, activate the option Advanced Behavior & Events and press the preview icon:
The code editor window will be available as a result of the previous steps:
The following sections explain the different alternatives to take advantage of this functionality of Prisma Campaigns.
Functions #
Set - With #
This function allows you to set the value of a field in a variety of ways. You can use a constant, another field, the result of a function, or an operation. This field can be taken through a data capture step on the page, or it can be part of the data model without participating in this operation. If the field in question is involved in the data capture, the value defined in the code will automatically appear when the step is executed.
The basic syntax of this function is set field <fieldID> with <expr>
, where the variants of <expr>
are explained below.
Set
-With
with a constant
In this case, <expr>
represents a defined value. For example, to set the value of balance
to 450:
set field balance with 450
Set
-With
with the value from another field
Under this scenario, you can use an existing value in a given field to assign the same to another. If you wish to store in balance
the same value as in taxes
, you can do the following:
set field balance with field taxes
It is important to note that taxes
does not necessarily have to be present in a data capture or multivalue inside of the page step. Instead, it can be any field from the overall or campaign data models.
Set
-With
with the result of a function
To illustrate this alternative, we will define two arbitrary functions named multiplyBy10(value)
and divideBy10(value)
:
function multiplyBy10(value) do
return value * 10
end
function divideBy10(value) do
return value / 10
end
These functions can be used to set the values of two fields interest
and taxes
very easily:
set field interest with multiplyBy10(500)
set field taxes with divideBy10(field extra)
After executing those two lines of code, interest
will be equal to 5000 whereas taxes
will store the result of dividing extra
by 10.
Set
-With
with the result of an operation
This option allows setting the value of a given field using any valid operation in the scripting language. For example, you can make balance
equal to taxes
plus 500:
set field balance with field taxes + 500
This alternative is a combination of the two first ones in that it makes it possible to set the value of a field using another and a constant.
Set - From #
Set
- From
iterates the execution of a function constantly during a page funnel step. If you wish to define a field based on another that belongs to a data capture or multivalue in the page, Set
- From
makes it possible to detect changes made in those controls immediately. The syntax is the same as for Set
- With
using from
instead of with
.
Set
-From
with the value of a field from a data capture in the page:
set field balance from field preBalance * 500
In this case, balance
will take the value from preBalance
times 500. When preBalance
changes in a data capture inside of the page, balance
will reflect the update in real time.
Set
-From
with the field of an active multivalue:
set field beneficiaries:name from "Jeff"
The example above shows how to assign the value Jeff
to the field name
of beneficiaries
.
Set
-From
with a field of an active multivalue and the value of another one in the same control:
set field beneficiaries:balance from field beneficiaries:extra * 10
In this case, the field balance
of the active multivalue beneficiaries
will store the value of extra
times 10.
Set - Visible When #
This function allows defining the visibility of a data capture or multivalue field inside of the page when a given condition is met. The syntax is set field <fieldID> visible when <expr>
where <fieldID>
represents the name of the field in question.
Set - Visible When
on a data capture field per a condition:
set field balance visible when field type == "account"
In this example, balance
will be visible if type
equals account
.
Set - Visible When
on a data capture field based on the return value of a function:
function setVisibleSum (value1, value2) do
sum = value1 + value2
if sum > 200 then
return true
end
end
set field revenue visible when fn(field income, 150)
When the sum of income
and 150 is greater than 200, revenue
will be visible as setVisibleSum
indicates.
Set - Visible When
using a field of an active multivalue and another one from the same control:
set field beneficiaries:phoneNumber visible when field beneficiaries:contact == "Phone"
In this case, phoneNumber
in beneficiaries
will be active if contact
equals Phone
.
Show/Hide #
If you need to set the visibility of a data capture or multivalue imperatively, the scripting language provides the alternatives shown in the following examples:
- Make the
tax
field visible:show field tax
- Enable
name
in multivaluebeneficiaries
:show field beneficiaries:name
- Hide the
record
field:hide field record
- Disable
income
in multivaluebeneficiaries
:hide field beneficiaries:income
When - Change #
This function makes it possible to execute actions after detecting changes in a data capture or multivalue field and to save the current value into a variable. The syntax is the following:
when field <fieldID> change as <variable> do
<expr>
end
where <variable>
is the variable that will take the current value of the field given by <fieldID>
.
When - Change
when detecting changes in a data capture field:
when field revenue change as revenueValue do
if revenueValue > 500 and field taxes > 200 then
show field beneficiaries
end
end
When revenue
changes, the new value will be stored in revenueValue
first. Next, beneficiaries
will be enabled if both conditions are met (revenueValue > 500
and taxes > 200
).
When - Change
after making changes in a field of an active multivalue:
when field project:revenue change as revenueValue do
if revenueValue > 500 and field project:taxes > 200 then
show field project:income
end
end
This example is similar to the previous one with the only difference that it involves the use of the field revenue
from project
.
When - Validate #
This function lets you run validations against the values entered in data capture or multivalue fields. The required syntax is as follows:
when field <fieldID> validate as <variable> do
<expr>
end
where <variable>
is the variable that will take the current value of the field given by <fieldID>
.
When - Validate
to execute a validation in a data capture field:
when field monthly_payment validate as mcost do
if mcost > field total_income then
return "Monthly Payment should not be greater than " + field total_income
end
end
In this case, the validation will run as illustrated in the image below if the value in monthly_payment
is greater than total_income
(200
in this example):
When - Validate
to validate a field from an active multivalue:
when field beneficiaries:percentage validate as perc do
if perc > 50 then
return "Percentage should not exceed 50%"
end
end
Given the code above, the validation will trigger if the value in the field percentage
of beneficiaries
is greater than 50.
Advanced Examples #
- Validation of a field associated with different elements of a multivalue:
when field beneficiaries validate as beneficiaries do
c = alen(beneficiaries)
i = 0
total = 0
while i < c do
beneficiary = aget(beneficiaries,i)
total = total + get(beneficiary,"percentage") # ex: {percentage: 40, name: 'john'}
i = i+1
end
if total > 100 then
return "The sum of percentages of all beneficiaries can't exceed 100"
end
end
In this example, the value of percentage
will be taken for each item in beneficiaries
. The validation will trigger if the total sum is greater than 100.