π What is SSJS in SFMC?
SSJS (Server-Side JavaScript) is used in Salesforce Marketing Cloud Engagement to run JavaScript code on SFMC servers π β not on the userβs browser.
β It’s useful for:
- π Personalizing landing pages (CloudPages)
- βοΈ Building dynamic emails
- π§ Handling complex logic
- π Interacting with Data Extensions
π§ Why Use SSJS over AMPscript?
| Feature | AMPscript β | SSJS π» |
|---|---|---|
| Inline personalization | β Best | β οΈ Works but complex |
| Arrays, try/catch, logic | β Limited | β Powerful |
| Easier for beginners | β Yes | β Slight learning curve |
| CloudPages & apps | β οΈ Limited | β Best |
π Use AMPscript for personalization in emails
π Use SSJS for logic-heavy work in CloudPages
π οΈ Syntax for SSJS Block
To write SSJS inside CloudPages, wrap it with:
<script runat="server">
// Your SSJS code here
</script>
π‘ Optional attributes:
<script
runat="server"
language="JavaScript"
executioncontexttype="Post"
executioncontextname="myContext">
// Your SSJS code here
</script>
π Real-Time Example: Retrieve Subscriber Info from Data Extension
π― Use Case
Retrieve a subscriber’s first name from a Data Extension based on their email address and display it on a CloudPage.
π§± Requirements
- A Data Extension named:
SubscribersDE- Columns:
EmailAddress,FirstName
- Columns:
- Create a CloudPage in SFMC
π» SSJS Code Block
<script runat="server">
Platform.Load("Core","1");
var email = Request.GetQueryStringParameter("email"); // Get email from URL param
var rows = DataExtension.Init("SubscribersDE").Rows.Retrieve({Property: "EmailAddress", SimpleOperator: "equals", Value: email});
var firstName = "Guest";
if(rows.length > 0) {
firstName = rows[0].FirstName;
}
</script>
<h1>Welcome, <ctrl:var name="firstName" default="Guest" />! π</h1>
π Step-by-Step: How to Run in SFMC
π§βπΌ Step 1: Create a Data Extension
- Go to Email Studio > Subscribers > Data Extensions
- Create
SubscribersDEwith:EmailAddress(Text, Primary Key)FirstName(Text)
π₯οΈ Step 2: Add Test Data
- Insert some records manually like:
| EmailAddress | FirstName |
|---|---|
| test@example.com | John |
| hello@domain.com | Alice |
π Step 3: Create a CloudPage
- Navigate to CloudPages
- Create a new Landing Page > Collection
- Drag in HTML Block
- Paste the SSJS code above
- Save & Publish the page
- Add a test query param like:
<script runat="server">
Platform.Load("Core", "1");
// Get email from URL parameter
var email = Request.GetQueryStringParameter("email");
// Initialize DE using CustomerKey (recommended over name)
var de = DataExtension.Init("48BA0"); // or use CustomerKey like "DE_KEY_123"
// Retrieve matching record
var rows = de.Rows.Retrieve({
Property: "EmailAddress",
SimpleOperator: "equals",
Value: email
});
// Default greeting name
var firstName = "Guest";
// If a match is found, use the FirstName from the DE
if (rows.length > 0) {
firstName = rows[0].FirstName;
}
// Output personalized greeting
Write("<h1>Welcome, " + firstName + "! π</h1>");
</script>

https://your-cloudpage-url.com?email=test@example.com
π It should display:
π Welcome, John! π

π§ͺ Bonus: Debugging & Tips
πΉ Platform.Load(“Core”,”1″)
Always load this before using Core functions.
πΉ Request.GetQueryStringParameter(“email”)
Fetches URL param. You can also use Request.GetFormField() for POST.
πΉ Error Handling Example
javascriptCopyEdittry {
// your code
} catch(e) {
Write("Error: " + Stringify(e));
}
π Wrap Up
β You just learned:
- What SSJS is in SFMC
- When to use SSJS vs AMPscript
- How to retrieve data from Data Extensions using SSJS
- How to build a dynamic CloudPage π‘
π Useful SFMC SSJS Methods Cheat Sheet
| Method | Use |
|---|---|
Retrieve |
Get data from Data Extensions |
Add |
Insert new record |
Update |
Update existing |
Remove |
Delete record |
Example:
jsCopyEditDataExtension.Init("SubscribersDE").Rows.Add({
EmailAddress: "new@domain.com",
FirstName: "NewUser"
});