code

Making a Query in Salesforce Using SSJS

Last updated: 27.10.2025
Views: 311

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.

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:

  • How to Remove WWW From a Website Address
    A redirect is to redirect site visitors from one URL to another. 301 status indicates that the redirect is permanent. Removing www from the site address is necessary prim...
  • Cheat sheet for work with Git
    Git is an indispensable tool for managing versions of code in development. It allows you to track changes in the project, return to previous versions and work effectively...
  • 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...

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 *