Making a Query in Salesforce Using SSJS
When working with Salesforce Marketing Cloud, you may need to send data to an external service or fetch content dynamically based on request parameters. Server-Side JavaScript (SSJS) gives you the flexibility to perform HTTP requests, handle responses, and manipulate JSON data directly within your cloud pages or emails. In the example below, we use Platform.Load to initialize the core library, retrieve a parameter from the request, and build a payload for a POST request. The script sends data to an external endpoint and checks the response status before proceeding. If the request is successful, it stores the result and parses it as JSON for further use, such as retrieving a specific value like totalCount. This technique is particularly useful when integrating external APIs or dynamic content into your campaigns.
Code
%%[
SET @infoblockId = RequestParameter("offerId")
]%%
<script runat="server">
Platform.Load("Core", "1")
var infoblockId = Variable.GetValue("@infoblockId");
var payload = {infoblockId: infoblockId};
//create request
var req = new Script.Util.HttpRequest('https//site.example/?param=some');
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = 'application/json';
req.method = "POST";
req.postData = Stringify(payload);
var res = req.send();
var respStatusCode = res.statusCode;
//if error
if (respStatusCode != 200) {
Variable.SetValue("@getRequest",{});
return;
}
//if success
Variable.SetValue("@getRequest",res.content);
//convert to JSON for special manipulation
var resultJSON = Platform.Function.ParseJSON(String(res.content));
var count = resultJSON.totalCount;
Variable.SetValue("@getcount",count);
</script>
Using SSJS in this way helps streamline data-driven personalization and real-time interactions in Marketing Cloud workflows.
Salesforce Marketing Cloud is a cloud-based digital marketing platform designed to help businesses manage and automate customer engagement across multiple channels. It provides tools for email marketing, mobile messaging, social media management, advertising, and customer journey orchestration.
The platform allows marketers to create personalized campaigns based on customer data, behavior, and preferences. With built-in analytics and automation features, it helps optimize communication and improve campaign performance. Salesforce Marketing Cloud is widely used by companies to deliver targeted, data-driven marketing experiences and maintain consistent interaction with their audience across different touchpoints.
Similar posts:
-
How to Remove WWW From a Website Address
When setting up a website, one of the small but important technical details is choosing a preferred domain version — with or without “www”. From an SEO perspective, this ...
-
Protecting a Website or Directory with a Password Using .htaccess and .htpasswd
Securing a website or a specific directory with a password is a simple yet effective way to restrict access. This can be done using .htaccess and .htpasswd files in Apach...
-
Configuring HTTPS for Nginx
HTTPS stands for Hypertext Transfer Protocol Secure, and it is the secure version of HTTP, the protocol used for communication between your web browser and a website. HTT...
Hello just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Internet explorer. I’m not sure if this is a format issue or something to do with web browser compatibility but I thought I’d post to let you know. The design look great though! Hope you get the issue solved soon. Many thanks