WSProxy helpers untested

Collection of useful code snippets (helpers) for managing requests using WSProxy library in SSJS (server-side JavaScript).

Table of contents

wsPack

Split an object into Name/Value pairs for WSProxy.

function wsPack(obj) {

    var result = [];

    for (var key in obj) {

        result.push({
            Name: key,
            Value: obj[key]
        });

    }

    return result;

}
<script runat="server">

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

    var api = new Script.Util.WSProxy();

	try {

        var record = {
            SubscriberKey: GUID(),
            FirstName: "John",
            LastName: "Doe",
            EmailAddress: "example@mail.com"
        }

        var result = api.createItem("DataExtensionObject", { 
            CustomerKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            Properties: wsPack(record)
        });

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

        Write(Stringify(error));

    }	

    function wsPack(obj) {

        var result = [];

        for (var key in obj) {

            result.push({
                Name: key,
                Value: obj[key]
            });

        }

        return result;

    }

</script>

wsUnpack

Merge and array of objects with Name/Value pairs into a single object.

function wsUnpack(props) {

    var result = {};

    for(var k in props) {

        var item = props[k];

        result[item.Name] = item.Value;

    }

    return result;

}
<script runat="server">

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

    var api = new Script.Util.WSProxy();

	try {

        var name = "MyDataExtension";

        var result = api.retrieve("DataExtensionObject[" + name + "]", ["EmailAddress"]);

        if(result.Results.length > 0) {

            var result = wsUnpack(result.Results[result.Results.length -1].Properties);

        }

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

        Write(Stringify(error));

    }	

    function wsUnpack(props) {

        var result = {};

        for(var k in props) {

            var item = props[k];

            result[item.Name] = item.Value;

        }

        return result;

    }

</script>

wsBatch

Compile data for a batch request that creates new records.

function wsBatch(customerKey, records) {

    var batch = [];

    for (var k in records) {

        batch.push({
            CustomerKey: customerKey,
            Properties: wsPack(records[k])
        });

    }

    return batch;

}
<script runat="server">

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

    var api = new Script.Util.WSProxy();

	try {

        var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var records = [
            {
                SubscriberKey: GUID(),
                FirstName: "John",
                LastName: "Doe",
                EmailAddress: "example1@mail.com"
            },
            {
                SubscriberKey: GUID(),
                FirstName: "Jane",
                LastName: "Doe",
                EmailAddress: "example2@mail.com"
            }
        ];

        var batches = wsBatch(customerKey, records);

        var result = api.createBatch("DataExtensionObject", batches);

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

        Write(Stringify(error));

    }	

    function wsBatch(customerKey, records) {

        var batch = [];

        for (var k in records) {

            batch.push({
                CustomerKey: customerKey,
                Properties: wsPack(records[k])
            });

        }

        return batch;

    }

    function wsPack(obj) {

        var result = [];

        for (var key in obj) {

            result.push({
                Name: key,
                Value: obj[key]
            });

        }

        return result;

    }

</script>

WARNING

This function only works in combination with wsPack function.


Last Updated: