Platform.Variable
The AMPscript–SSJS variable bridge. Read and write AMPscript variables from within SSJS script blocks.
Platform.Variable provides two methods for bridging between AMPscript and SSJS execution contexts. Any variable set in AMPscript can be read in SSJS, and vice versa.
Methods
| Method | Returns | Description |
|---|---|---|
Platform.Variable.GetValue(name) |
string | Reads the value of an AMPscript variable |
Platform.Variable.SetValue(name, value) |
void | Writes a value to an AMPscript variable |
GetValue
Syntax
Platform.Variable.GetValue(variableName)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
variableName |
string | Yes | AMPscript variable name, prefixed with @ |
Return Value
Returns the variable’s value as a string. Returns "" (empty string) if the variable is not set.
Examples
// Read an AMPscript variable
var email = Platform.Variable.GetValue("@email");
var firstName = Platform.Variable.GetValue("@firstName");
SetValue
Syntax
Platform.Variable.SetValue(variableName, value)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
variableName |
string | Yes | AMPscript variable name, prefixed with @ |
value |
any | Yes | Value to assign |
Examples
Platform.Variable.SetValue("@result", "processed");
Platform.Variable.SetValue("@count", rows.length);
Platform.Variable.SetValue("@jsonPayload", Stringify(data));
After SetValue, the variable is accessible in subsequent AMPscript expressions on the same page:
%%[ /* AMPscript reading the value set above */ ]%%
Your result: %%=v(@result)=%%
Common Patterns
Pass SSJS computation to AMPscript rendering
// Compute in SSJS
var score = computeLeadScore(subscriberKey);
Platform.Variable.SetValue("@leadScore", score);
%%[ /* Use in AMPscript conditional block */ ]%%
%%[ IF @leadScore > 80 THEN ]%%
<strong>High priority lead</strong>
%%[ ENDIF ]%%
Read AMPscript personalization in SSJS
%%[
SET @subKey = _subscriberkey
SET @email = emailaddr
]%%
<script runat="server">
var subKey = Platform.Variable.GetValue("@subKey");
var email = Platform.Variable.GetValue("@email");
// use subKey and email in SSJS logic
</script>
Safe encoding via AMPscript
Variable.SetValue("@rawInput", userInput);
TreatAsContent("%%[SET @encoded = URLEncode(@rawInput, 1, 1)]%%");
var encoded = Variable.GetValue("@encoded");
The global Variable object is an alias for Platform.Variable. Both Variable.GetValue() and Platform.Variable.GetValue() are equivalent.