code

Making a Query in Salesforce Using SSJS

Last updated: 09.04.2026
Views: 371

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.

author
Author: Igor Rybalko
I have been working as a front-end developer since 2014. My main technology stack is Vue.js and WordPress.

Similar posts:

One response to “Making a Query in Salesforce Using SSJS”

  1. zoritoler imol says:

    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

Leave a Reply

Your email address will not be published. Required fields are marked *