SSJS Syntax Guide
Use the information in this document to correctly form SSJS function calls, declare variables and values, and reference keywords.
Use Script tags to delineate JavaScript blocks. These tags are processed as case-insensitive instances. Quotation marks are optional unless they're necessary to distinguish a value.
Marketing Cloud Engagement executes SSJS at the time the content is rendered.
When you use SSJS, you must specify that the code is run at the server, as shown in this example. If you don't specify this parameter, the browser tries to execute it, which results in errors.
You can also specify additional parameters.
For the Language
parameter, you can specify either JavaScript
or AMPscript
. The default value is JavaScript.
For ExecutionContextType
, you can specify either Get
or Post
. The default value is Get
.
For ExecutionContextName
, you can specify a named context to execute.
SSJS uses personalization tokens for subscriber and system attributes, data extension fields, variables, functions, and JavaScript code expressions.
Use these tags as part of your personalization strings:
ctrl:field
- References subscriber attribute values, system attribute values, and sendable data extension field values.ctrl:var
- References SSJS or AMPscript variables.ctrl:eval
- Embeds JavaScript expressions as content substitutions.
You can include these attributes in your personalization strings:
name
- Identifies the attribute, sendable data extension field, or variable. Include this value for all attribute and variable data sources.default
- Provides an optional default value for all tags. The expression returns this value if the data source is null or an empty string.format
- Specifies a format for the personalization string. SSJS supports standard .NET numeric and datetime strings.language
- Specifies the language of the expression. Accepted values arejavascript
andampscript
. The default value isjavascript
.
Use these examples as models for constructing your own SSJS code. Use this code anywhere in a landing page to provide personalized information.
Use the ctrl:field
tag to reference subscriber information or information from a sendable data extension.
You can optionally include the default
parameter to specify a default value.
Access an SSJS variable by specifying its name.
To access an AMPscript variable, include the at sign (@) in the variable name.
You can include the default
attribute to provide a value if the variable is not found. You can also include the format
variable to apply a format to the value of the variable.
Use the ctrl:eval
tag to evaluate a JavaScript expression.
You can use full-function syntax or the equivalent simple expression syntax. For example, both of these expressions produce the same result.
SSJS interacts with information from Marketing Cloud Engagement by using libraries that are unique to SSJS. To use these libraries in your code, use the Platform.Load()
function to load them.
SSJS supports these methods:
- Add - Invokes the web service API Create method on an API object
- Remove - Invokes the web service API Delete method on an API object
- Update - Invokes the web service API Update method on an API object
- Retrieve - Invokes the web service API Retrieve method on an API object
SSJS can only access the .NET CLR (Common Language Runtime) objects exposed through the outlined libraries. Attempts to access disallowed CLR objects result in an error.