JavaScript code javascript-code
The JavaScript code activity is a Data management activity. Use this activity to execute a JavaScript script in the context of a workflow. This enables you to collect information from the database or perform other complex operations.
Configure the JavaScript code activity javascript-code-configuration
Follow these steps to configure the JavaScript code activity:
-
Add a JavaScript code activity into your workflow.
-
Choose the type of activity to create:
- Simple: Execute one code snippet.
- Advanced: Perform more advanced operations by executing two different code snippets. Learn how to configure an advanced JavaScript activity
note note NOTE With the Campaign Web User Interface, two activities have been consolidated into one by merging both Simple and Advanced JavaScript code functionalities. This consolidation does not impact the activity鈥檚 functionality. -
Confirm, then click the Edit code button to open the expression editor. The left-hand pane provides predefined syntaxes that you can use to build your code, including event variables. Learn how to work with event variables and the expression editor.
-
In the Execution section, configure the delay to stop the activity after a period of execution. By default, the execution phase cannot exceed 1 hour. After this delay, the process is aborted with an error message and the activity execution fails. To ignore this limit, set the value to 0.
-
Toggle on the Process errors option to keep errors that occur during the script execution in an additional output transition.
Advanced JavaScript code activities advanced
Advanced JavaScript activities allow you to perform complex operations. These activities enable you to:
- Execute two different code snippets. The first code snippet is executed the first time the workflow starts. Each time the workflow runs again, the code snippet defined in the second call is executed.
- Add multiple output transitions that you can dynamically interact with using a script.
To configure an Advanced JavaScript code activity, follow these steps:
-
Select the Advanced type, then configure the code snippets to execute:
- Click Edit first call code to define the script to execute during the first call.
- Click Edit next call code to define the script to execute during subsequent calls of the workflow. (optional)
-
To add one or multiple output transitions, click the Add transitions button and specify a label and an internal name for each transition.
In this example, two transitions are configured and activated by the script in the code snippet based on specific conditions.
-
Complete the activity configuration and start the workflow.
Example javascript-code-example
Initialize variables based on the incoming population example1
This example shows how to initialize a variable based on the number of profiles targeted by a workflow.
In this example, VIP profiles from the database are targeted. A variable named 鈥渃hannel鈥 is created with a value that depends on the number of profiles targeted by the Build audience activity:
- If more than 1000 profiles are targeted, initialize the variable with the value 鈥渆mail鈥.
- Otherwise, initialize it with the value 鈥渟ms鈥.
To achieve this, follow these steps:
-
Add a JavaScript code activity with the type Simple after the Build audience activity.
-
Click Edit code and configure the code snippet as shown below:
code language-none if (vars.recCount > 1000) vars.channel = "email"; else vars.channel = "sms";
-
Start the workflow. The 鈥渃hannel鈥 variable is created with the 鈥渆mail鈥 or 鈥渟ms鈥 value, depending on the number of profiles targeted by the Build audience activity.
Trigger transitions based on a variable鈥檚 value example2
This example shows how to trigger a transition based on the value of a variable.
In this example, the workflow starts with an External signal activity, into which a variable (interest
) is passed from another workflow. The value of the variable is either 鈥渞unning鈥 or 鈥測oga鈥, depending on the filtering operations performed in the initial workflow.
Different transitions in the workflow are triggered based on the variable鈥檚 value.
To achieve this, follow these steps:
-
Add a JavaScript code activity after the External signal activity with the type Advanced.
-
Add two transitions: one for each possible variable value (鈥渞unning鈥, 鈥測oga鈥).
-
Click Edit first call code and configure the code snippet as shown below:
code language-none if (vars.interest == "running") task.postEvent(task.transitionByName("running")); else task.postEvent(task.transitionByName("yoga"));
-
Complete the configuration of each transition to suit your needs, then start the workflow. One of the two output transitions is activated based on the value of the
interest
variable passed through the External signal activity.