Util · Variable · Request — Marketing Cloud SSJS reference
The bridges between SSJS and the surfaces around it — Variable for AMPscript interop, Request for CloudPage URL/form access, Platform.Function.RaiseError + GetSetting for control flow and config. The grab-bag page that ties the catalog to its neighbors.
This page covers the SSJS surfaces that don't fit any other category but show up constantly in production: the Variable namespace for swapping values with AMPscript inside email script blocks, the Request object for reading URL params and form fields in CloudPages, Platform.Function.RaiseError for surfacing errors back to the caller, and Platform.Function.GetSetting for reading tenant configuration. Small surface, high frequency of use — and the one chapter where SSJS stops being a self-contained language and starts being a glue runtime between AMPscript, the request lifecycle, and the SFMC environment.
Official syntax
Variable.* — AMPscript ↔ SSJS interop
Used in email script blocks where AMPscript and SSJS coexist. AMPscript variables are prefixed with @; SSJS reads/writes them through Variable.GetValue / Variable.SetValue.
%%[ /* AMPscript declares + sets */
VAR @firstName, @loyaltyTier
SET @firstName = AttributeValue("FirstName")
SET @loyaltyTier = "gold"
]%%
<script runat="server" language="javascript">
Platform.Load("Core", "1.1.5");
// SSJS reads what AMPscript set
var name = Variable.GetValue("@firstName");
var tier = Variable.GetValue("@loyaltyTier");
// SSJS writes a value AMPscript can read after this script block
var greeting = "Hi " + name + ", you're tier " + tier;
Variable.SetValue("@greeting", greeting);
</script>
%%[ /* AMPscript reads what SSJS set */
Output(v(@greeting))
]%%The interop is per-message, per-script-block. The values don't persist to a Data Extension automatically; they live in the personalization context for that send.
Request.* — CloudPage URL + form access
Used in CloudPage SSJS where a user request reaches the page. The Request object exposes the request lifecycle.
Platform.Load("Core", "1.1.5");
// Query string parameters (?email=...&token=...)
var email = Request.GetQueryStringParameter("email");
var token = Request.GetQueryStringParameter("token");
// Form fields (POST submissions to the same page)
var formEmail = Request.GetFormField("email");
// Request metadata
var ua = Request.GetUserAgent();
var ip = Request.GetUserHostName();
var referrer = Request.GetReferrer();
var method = Request.Method; // "GET" or "POST"
// Raw request body (for webhooks / API endpoints rendered as CloudPages)
var rawBody = Platform.Request.GetPostData();Platform.Function.RaiseError — surface errors back to the caller
The cleanest way to fail a Script Activity step (or render an error in a CloudPage) is to raise an explicit error. The Activity's run log captures the message; the Automation can be configured to halt on the failure or continue.
Platform.Load("Core", "1.1.5");
if (rows.length === 0) {
// Halt the script with an explicit error message
Platform.Function.RaiseError("No subscribers matched filter — aborting send build.", true);
// The 'true' second argument includes the call stack in the error.
}For inline error capture without halting, the older Stack.Push idiom still works in some tenants:
try {
Platform.Function.UpsertData("master_subs", ["SubscriberKey"], [key], cols, vals);
} catch (e) {
// Log to a DE
Platform.Function.UpsertData(
"de_log_errors",
["RunId"], [runId],
["Step","Msg","Ts"], ["upsert", e.message, Now()]
);
// Decide: re-raise or swallow
Platform.Function.RaiseError("Upsert failed: " + e.message, true);
}Platform.Function.GetSetting — read tenant settings
Some installation-time configuration is exposed via GetSetting. Useful for environment-aware logic without hardcoding values.
Platform.Load("Core", "1.1.5");
var instance = Platform.Function.GetSetting("instance"); // e.g. "s10.exacttarget.com"
var account = Platform.Function.GetSetting("MID"); // numeric MIDThe exact set of available setting names depends on the tenant. Consult the Salesforce Help docs for your edition.
Reference summary
| Function | Context | Returns | Use for |
|---|---|---|---|
| Variable.GetValue("@name") | Email script block | String / value | Read AMPscript var into SSJS |
| Variable.SetValue("@name", v) | Email script block | (void) | Write SSJS value into AMPscript var |
| Request.GetQueryStringParameter(name) | CloudPage | String / null | Read URL query param |
| Request.GetFormField(name) | CloudPage | String / null | Read POST form field |
| Request.GetUserAgent() | CloudPage | String | Browser UA |
| Request.GetUserHostName() | CloudPage | String | Client IP / hostname |
| Request.GetReferrer() | CloudPage | String | HTTP Referer header |
| Request.Method | CloudPage | "GET" / "POST" | HTTP method |
| Platform.Request.GetPostData() | CloudPage | String | Raw request body |
| Platform.Function.RaiseError(msg, includeStack) | Anywhere | (throws) | Halt with explicit error |
| Platform.Function.GetSetting(name) | Anywhere | String / value | Read tenant configuration |
Reference:
- Salesforce Developer — Platform Functions reference (RaiseError, GetSetting) ↗
- Salesforce Developer — SSJS overview (Variable + Request) ↗
What survives in production
Request.GetQueryStringParameter returns null for missing params — not empty string
A common bug shape: code that assumes a missing param will be "" and concatenates it into a URL or DE write. The result is the literal string "null" (because of how + coerces — see String functions).
// AT RISK — if 'email' isn't in the URL, log message becomes "User: null"
var email = Request.GetQueryStringParameter("email");
Write("User: " + email);
// SAFE — coalesce missing values explicitly
var email = Request.GetQueryStringParameter("email") || "";
// Or with explicit guard
var email = Request.GetQueryStringParameter("email");
if (email == null) {
Platform.Function.RaiseError("Missing required 'email' query param.", true);
}The right choice depends on whether the param is required (raise an error) or optional (default to "" or some sentinel).
Always validate user input before using it in DE writes or URL building
Anything from Request.GetQueryStringParameter or Request.GetFormField is untrusted. SSJS doesn't escape it for you. Length, character set, and format have to be checked explicitly before the value reaches a DE write or a redirect URL.
// AT RISK — user-supplied 'next' parameter pasted into redirect, no validation
var next = Request.GetQueryStringParameter("next");
Platform.Response.Redirect(next);
// Open redirect vulnerability: an attacker can craft ?next=https://evil.com
// and your CloudPage redirects to their site
// SAFE — whitelist allowed redirect targets
var next = Request.GetQueryStringParameter("next");
var allowed = ["/preferences", "/done", "/help"];
var safeNext = "/done";
for (var i = 0; i < allowed.length; i++) {
if (next === allowed[i]) { safeNext = next; break; }
}
Platform.Response.Redirect(safeNext);The discipline: never pass user input directly to a redirect, a DE write, or a SQL Activity input without validation. The list of allowed values is short for most use cases; a whitelist beats a blacklist.
Variable.GetValue returns whatever AMPscript set, including null and empty string
The interop is faithful — if AMPscript set the variable to an empty string, SSJS reads "". If AMPscript never set it, SSJS reads null. If AMPscript set it to a number, SSJS reads a number (not a string). Don't assume the type.
// AT RISK — assumes Variable.GetValue returns a string
var tier = Variable.GetValue("@loyaltyTier");
if (tier.toLowerCase() === "gold") { // throws if tier is null
// ...
}
// SAFE — guard for null + coerce to string
var tier = Variable.GetValue("@loyaltyTier");
tier = tier == null ? "" : String(tier);
if (tier.toLowerCase() === "gold") {
// ...
}Platform.Function.RaiseError halts the script — design for it
RaiseError doesn't just log the error; it throws out of the script. Anything after the call doesn't run. Inside an Automation Script Activity, this fails the step and the Automation reacts based on its configuration (halt vs continue).
// The script halts at RaiseError. The DE write never happens.
Platform.Function.RaiseError("Stopping for review.", true);
Platform.Function.UpsertData("de_log_runs", ...); // never reached
// If you want to log first then halt, write the log BEFORE RaiseError
Platform.Function.UpsertData(
"de_log_runs",
["RunId"], [runId],
["Step","Msg","Ts"], ["abort", "Stopping for review", Now()]
);
Platform.Function.RaiseError("Stopping for review.", true);The order matters — log before raising, because the raise is a one-way exit.
Quick decision
Use Variable.GetValue / SetValue when:
- Inside an email script block. AMPscript declares + reads the variables, SSJS does the heavy logic.
Use Request.GetQueryStringParameter when:
- Inside a CloudPage. Reading URL params from a user-clicked link, redirect, or webhook.
Use Request.GetFormField when:
- Inside a CloudPage that accepts POST submissions (preference centers, suppression forms).
Use Platform.Function.RaiseError when:
- You want to halt a Script Activity with a clear error message that surfaces in the Activity log.
- The script reached a state where continuing would corrupt downstream data.
Use Platform.Function.GetSetting when:
- Reading tenant-level configuration that varies per environment (BU MID, instance, etc.).
Always validate user input from Request.* when:
- The value reaches a redirect URL, a DE write, or a Send recipient list. No exceptions.
Related
- Basics — supported language surface
- Platform.Function — main API namespace, where RaiseError + GetSetting live
- String functions —
+coercion ofnullto"null"(related toRequest.GetQueryStringParameterreturningnull) - Encoding functions — URL-encode
Request.*values before re-emitting in URLs - MC SSJS gotchas — see #7 (silent try/catch) for the error-surfacing context
More SSJS reference pages incoming: Style Guide.
Plus how-to snippets for common production patterns — DE add/update/upsert, error handling, callout pagination, etc.