Work with JavaScript codes
About JavaScript codes
JavaScript codes allow you to create reusable functions that can be used across workflows, similar to a library. These functions are stored in the Administration > JavaScript codes menu in the left navigation pane.
From the JavaScript codes list, you can:
- Duplicate or delete a code: Click the ellipsis button, and select the desired action.
- Modify a code: Click a code’s name to open its properties, make your changes, and save.
- Create a new JavaScript code: Click the Create JavaScript code button.
Create a JavaScript code
To create a JavaScript code, follow these steps:
-
Navigate to the JavaScript codes menu, and click the Create JavaScript code button.
-
Define the code’s properties:
- Namespace: Specify the namespace relevant to your custom resources. By default, the namespace is “cus”, but it may vary depending on your implementation.
- Name: The unique identifier used to reference the code.
- Label: The descriptive label displayed in the JavaScript codes list.
NOTE
Once created, the Namespace and Name fields cannot be modified. To make changes, duplicate the code and update as needed.In the Campaign console, the JavaScript code name appears as a concatenation of these two fields. -
Click the Create code button to define the JavaScript code. The left pane provides two menus that allow you to use predefined functions related to conditions and date formatting.
-
Click Confirm to save your code.
-
When your JavaScript code is ready, click Create. The JavaScript code is now available for use across workflows.
Use a JavaScript code from a workflow
Load JavaScript code libraries
You can reference JavaScript codes in workflows to avoid rewriting code for repetitive tasks. To use these codes, load the corresponding library in the workflow’s initialization script. This allows you to load all the libraries containing the functions you want to use in your workflow once.
To load a library, follow these steps:
-
Open a workflow, and click the Settings button.
-
Navigate to the Initialization script section, and click Create code.
-
Use the syntax below in the code to load a library:
loadLibrary("/<namespace>/<name>")
- Replace
<namespace>
with the namespace specified during the JavaScript code creation. - Replace
<name>
with the name of the JavaScript code.
- Replace
-
Click Confirm, and save the settings.
Reference functions in workflows
Once the JavaScript library is loaded, you can reference the functions defined in the JavaScript code directly within the workflow, typically using a JavaScript code activity.