Salesforce helpers untested

Collection of useful code snippets (helpers) for native Salesforce functions in SSJS (server-side JavaScript).

Table of contents

Create Salesforce Object

function createSalesforceObject(name, payload) {

    var i = 0;

    var scr = "";
        scr += "\%\%[";
        scr += "SET @Id = CreateSalesforceObject( ";
        scr += "'" + name + "', ";
        scr += (payload.length + 1) + ", ";

        for(var k in payload) {

            i++;

            var nm = k;
            var val = payload[k];

            val = val.replace(/'|’/g, "'");

            scr += "'" + nm + "', ";
            scr += '"' + val + '" ';

            if(i < payload.length + 1) scr += ", ";

        }

        scr += ") ";
        scr += "OUTPUT(CONCAT(@Id)) ";
        scr += "]\%\%";

    var res = Platform.Function.TreatAsContent(scr);

    return res

}
<script runat="server">

    Platform.Load("core", "1");
	
	try {

        var payload = {
            FirstName: "John",
            LastName: "Doe",
            Custom_Date: formatDateISO("2023-03-03")
        };
		
        var result = createSalesforceObjects("Individual", payload);	

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }
    
    function formatDateISO(dt) {

        Variable.SetValue("DateString", dt);

        var scr = "";
            scr += "\%\%[";
            scr += "SET @DateISO = FormatDate(@DateString, 'iso')";
            scr += "Output(Concat(@DateISO))";
            scr += "]\%\%";

        return Platform.Function.TreatAsContent(scr);
    }
	
    function createSalesforceObjects(name, payload) {

        var i = 0;

        var scr = "";
            scr += "\%\%[";
            scr += "SET @Id = CreateSalesforceObject( ";
            scr += "'" + name + "', ";
            scr += (payload.length + 1) + ", ";

            for(var k in payload) {

                i++;

                var nm = k;
                var val = payload[k];

                val = val.replace(/'|’/g, "'");

                scr += "'" + nm + "', ";
                scr += '"' + val + '" ';

                if(i < payload.length + 1) scr += ", ";

            }

            scr += ") ";
            scr += "OUTPUT(CONCAT(@Id)) ";
            scr += "]\%\%";

        var res = Platform.Function.TreatAsContent(scr);

        return res

    }

</script>

Update Single Salesforce Object

function updateSingleSalesforceObject(name, id, payload) {

    var i = 0;

    var scr = "";
        scr += "\%\%[";
        scr += "SET @Id = UpdateSingleSalesforceObject( ";
        scr += "'" + name + "', ";
        scr += "'" + id + "', ";

        for(var k in payload) {

            i++;

            var nm = k;
            var val = payload[k];

            val = val.replace(/'|’/g, "'");

            scr += "'" + nm + "', ";
            scr += '"' + val + '" ';

            if(i < payload.length + 1) scr += ", ";

        }

        scr += ") ";
        scr += "OUTPUT(CONCAT(@Id)) ";
        scr += "]\%\%";

    var res = Platform.Function.TreatAsContent(scr);

    return res

}
<script runat="server">

    Platform.Load("core", "1");
	
	try {

        var payload = {
            FirstName: "Jane",
            LastName: "Doe"
        };
		
        var result = updateSingleSalesforceObject("Contact", "S0ME-SF-ID", payload);	

        Write(Stringify(result));
		
	} catch(error) {

        Write(Stringify(error));
        
    }
	
    function updateSingleSalesforceObject(name, id, payload) {

        var i = 0;

        var scr = "";
            scr += "\%\%[";
            scr += "SET @Id = UpdateSingleSalesforceObject( ";
            scr += "'" + name + "', ";
            scr += "'" + id + "', ";

            for(var k in payload) {

                i++;

                var nm = k;
                var val = payload[k];

                val = val.replace(/'|’/g, "'");

                scr += "'" + nm + "', ";
                scr += '"' + val + '" ';

                if(i < payload.length + 1) scr += ", ";

            }

            scr += ") ";
            scr += "OUTPUT(CONCAT(@Id)) ";
            scr += "]\%\%";

        var res = Platform.Function.TreatAsContent(scr);

        return res

    }

</script>

Retrieve Salesforce Object

function retrieveSalesforceObjects(name, cols, filters) {

    var filterStr = "";

    var colStr = cols.join(", ");

    for(var k in filters) {

        var filter = filters[k];

        filter.Value = filter.Value.replace(/'|’/g, "'");

        filterStr += "'" + filter.Property + "', ";
        filterStr += "'" + ((filter.SimpleOperator == "equals") ? "=" : "!=") + "', ";
        filterStr += "\"" + filter.Value + "\"";

        if(k < filters.length - 1) filterStr += ", ";

    }

    var scr = "";
        scr += "\%\%[";
        scr += "SET @Result = '[' ";
        scr += "SET @Rows = RetrieveSalesforceObjects('" + name + "', '" + colStr + "', " + filterStr + ") ";
        scr += "SET @RowCount = ROWCOUNT(@Rows) ";
        scr += "IF @RowCount > 0 THEN ";
        scr += "FOR @i = 1 TO @RowCount DO ";
        scr += "SET @Row = Row(@Rows, @i) ";
        scr += "SET @Result = CONCAT(@Result, '{') ";

        for(var i = 0; i < cols.length; i++) {

            var col = cols[i];

            scr += "SET @Result = CONCAT(@Result,'\"" + col + "\": ', '\"', FIELD(@Row, '" + col + "'), '\"') "; 

            if(i < cols.length - 1) scr += "SET @Result = CONCAT(@Result,', ') ";

        }

        scr += "SET @Result = CONCAT(@Result, '}') ";
        scr += "IF @i != @RowCount THEN ";
        scr += "SET @Result = CONCAT(@Result,', ') ";
        scr += "ENDIF ";
        scr += "NEXT @i ";
        scr += "ENDIF ";
        scr += "SET @Result = CONCAT(@Result, ']') ";
        scr += "OUTPUT(CONCAT(@Result)) ";
        scr += "]\%\%";

    var res = Platform.Function.TreatAsContent(scr);

    return Platform.Function.ParseJSON(res)

}
<script runat="server">

    Platform.Load("core", "1");
	
	try {

        var cols = ["Id", "Salutation", "AccountId"];

        var filters = [
            {
                Property: "Email", 
                SimpleOperator: "equals", 
                Value: "example@mail.com" 
            },
            {
                Property: "LastName", 
                SimpleOperator: "equals", 
                Value: "Doe" 
            }
        ];

        var result = retrieveSalesforceObject("Contact", cols, filters);
		
	    Write(Stringify(result));	
		
	} catch(error) {

        Write(Stringify(error));
        
    }	

    function retrieveSalesforceObject(name, cols, filters) {

        var filterStr = "";

        var colStr = cols.join(", ");

        for(var k in filters) {

            var filter = filters[k];

            filter.Value = filter.Value.replace(/'|’/g, "'");

            filterStr += "'" + filter.Property + "', ";
            filterStr += "'" + ((filter.SimpleOperator == "equals") ? "=" : "!=") + "', ";
            filterStr += "\"" + filter.Value + "\"";

            if(k < filters.length - 1) filterStr += ", ";

        }

        var scr = "";
            scr += "\%\%[";
            scr += "SET @Result = '[' ";
            scr += "SET @Rows = RetrieveSalesforceObjects('" + name + "', '" + colStr + "', " + filterStr + ") ";
            scr += "SET @RowCount = ROWCOUNT(@Rows) ";
            scr += "IF @RowCount > 0 THEN ";
            scr += "FOR @i = 1 TO @RowCount DO ";
            scr += "SET @Row = Row(@Rows, @i) ";
            scr += "SET @Result = CONCAT(@Result, '{') ";

            for(var i = 0; i < cols.length; i++) {

                var col = cols[i];

                scr += "SET @Result = CONCAT(@Result,'\"" + col + "\": ', '\"', FIELD(@Row, '" + col + "'), '\"') "; 

                if(i < cols.length - 1) scr += "SET @Result = CONCAT(@Result,', ') ";

            }

            scr += "SET @Result = CONCAT(@Result, '}') ";
            scr += "IF @i != @RowCount THEN ";
            scr += "SET @Result = CONCAT(@Result,', ') ";
            scr += "ENDIF ";
            scr += "NEXT @i ";
            scr += "ENDIF ";
            scr += "SET @Result = CONCAT(@Result, ']') ";
            scr += "OUTPUT(CONCAT(@Result)) ";
            scr += "]\%\%";

        var res = Platform.Function.TreatAsContent(scr);

        return Platform.Function.ParseJSON(res)

    }

</script>

Last Updated: