SSJS essentials for Salesforce Marketing Cloud
Use this reference as your SSJS learning hub. Each section includes trusted SFMC patterns, best practices, and code snippets you can reuse in Cloud Pages, Script Activities, and API integrations.
Tip: In Marketing Cloud, Server-Side JavaScript runs inside the <script runat="server"> context and exposes key objects like Platform, DataExtension, and HTTP.
Data Extension API
Retrieve, insert, update, and delete rows using DataExtension.Init() and the Rows APIs.
<script runat="server">
var de = DataExtension.Init("Subscribers");
var rows = de.Rows.Retrieve({Property:"Status", SimpleOperator:"equals", Value:"Active"});
for (var i = 0; i < rows.length; i++) {
Platform.Response.Write(rows[i].EmailAddress + "<br>");
}
</script>
Use Rows.Update() to update, Rows.Add() to insert, and Rows.Remove() to delete rows.
Platform.Function namespace
Platform.Function gives you access to Marketing Cloud functions from SSJS, including ParseJSON, FormatDate, Base64Encode, and more.
<script runat="server">
var payload = '{"name":"Mia","score":92}';
var obj = Platform.Function.ParseJSON(payload);
var encoded = Platform.Function.Base64Encode(obj.name);
Platform.Response.Write(encoded);
</script>
HTTP requests and API integration
Use HTTP.Get and HTTP.Post for outbound REST integration, authorization, and external API calls.
<script runat="server">
var url = "https://api.example.com/data";
var headerNames = ["Authorization"];
var headerValues = ["Bearer YOUR_TOKEN"];
var result = HTTP.Get(url, headerNames, headerValues);
var responseBody = result.Response[0];
Platform.Response.Write(responseBody);
</script>
Cloud Page scripting
Access request parameters and render personalized content inside Cloud Pages using Request.GetQueryStringParameter and Portal functions.
<script runat="server">
var name = Request.GetQueryStringParameter("name");
if (!name) {
name = "Visitor";
}
Platform.Response.Write("Hello " + name + "!");
</script>
Automation Studio Script Activity
Build reusable SSJS scripts for scheduled automation tasks, data updates, and batch processing.
<script runat="server">
var de = DataExtension.Init("WeeklySummary");
var rows = de.Rows.Retrieve();
for (var i = 0; i < rows.length; i++) {
// process each row
}
</script>
Script Activity runs once per Automation and can access SSJS APIs available to the Marketing Cloud backend.
Debugging and error handling
Wrap code in try/catch and write contextual errors for faster debugging in Cloud Pages and script logs.
<script runat="server">
try {
var de = DataExtension.Init("ExampleDE");
var rows = de.Rows.Retrieve();
} catch (e) {
Platform.Response.Write("SSJS error: " + String(e));
}
</script>