Create a DataExtension record verified

Learn how to create Data Extension records (rows) in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include WSProxy, Core, Platform and REST API methods.

Table of contents

Platform

var config = {
    dataextension: "MyDataExtension",
    attributes: {
        Names: [
            "SubscriberKey", 
            "EmailAddress", 
            "FirstName", 
            "LastName"
        ],
        Values: [
            Platform.Function.GUID(), 
            "example@mail.com", 
            "John", 
            "Doe"
        ]
    }
}

var result = Platform.Function.InsertData(
    config.dataextension,
    config.attributes.Names,
    config.attributes.Values
);

Platform.Response.Write(Platform.Function.Stringify(result));
<script runat="server">
	try {

        var config = {
            dataextension: "MyDataExtension",
            attributes: {
                Names: [
                    "SubscriberKey", 
                    "EmailAddress", 
                    "FirstName", 
                    "LastName"
                ],
                Values: [
                    Platform.Function.GUID(), 
                    "example@mail.com", 
                    "John", 
                    "Doe"
                ]
            }
        }

        var result = Platform.Function.InsertData(
            config.dataextension,
            config.attributes.Names,
            config.attributes.Values
        );

        Platform.Response.Write(Platform.Function.Stringify(result));
		
	} catch(error) {
        Platform.Response.Write(Platform.Function.Stringify(error));
    }
</script>
1

Core

var de = DataExtension.Init("MyDataExtension");
	
var payload = {
    SubscriberKey: GUID(),
    FirstName : "John",
    LastName : "Doe",
    EmailAddress : "example@mail.com" 
};

var result = de.Rows.Add(payload);
<script runat="server">

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

	try {

        var de = DataExtension.Init("MyDataExtension");
	
        var payload = {
            SubscriberKey: GUID(),
            FirstName : "John",
            LastName : "Doe",
            EmailAddress : "example@mail.com" 
        };

        var result = de.Rows.Add(payload);

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

</script>
1

WSProxy

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

var result = api.createItem("DataExtensionObject", { 
    CustomerKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    Properties: [
        {
            Name: "SubscriberKey",
            Value: GUID()
        },
        {
            Name: "FirstName",
            Value: "John"
        },
        {
            Name: "LastName",
            Value: "Doe"
        },
        {
            Name: "EmailAddress",
            Value: "example@mail.com"
        }
    ] 
});
<script runat="server">

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

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

	try {

        var result = api.createItem("DataExtensionObject", { 
            CustomerKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            Properties: [
                {
                    Name: "SubscriberKey",
                    Value: GUID()
                },
                {
                    Name: "FirstName",
                    Value: "John"
                },
                {
                    Name: "LastName",
                    Value: "Doe"
                },
                {
                    Name: "EmailAddress",
                    Value: "example@mail.com"
                }
            ] 
        });

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

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "ErrorMessage": null,
            "KeyErrors": null,
            "ValueErrors": null,
            "NewID": 0,
            "NewObjectID": null,
            "PartnerKey": null,
            "Object": {
                "Name": null,
                "Keys": null,
                "Type": null,
                "Properties": [
                    {
                        "Name": "SubscriberKey",
                        "Value": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
                    },
                    {
                        "Name": "FirstName",
                        "Value": "John"
                    },
                    {
                        "Name": "LastName",
                        "Value": "Doe"
                    },
                    {
                        "Name": "EmailAddress",
                        "Value": "example@mail.com"
                    }
                ],
                "Client": null,
                "PartnerKey": null,
                "PartnerProperties": null,
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "ObjectID": null,
                "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "CreateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Created DataExtensionObject",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

REST API

Synchronous

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

var payload = [
    {
        "keys": {
            "SubscriberKey": GUID()
        },
        "values": {
            "FirstName": "John",
            "LastName": "Doe"
        }
    },
    {
        "keys": {
            "SubscriberKey": GUID()
        },
        "values": {
            "FirstName": "Jane",
            "LastName": "Doe"
        }
    }
];

var endpoint = restInstanceUrl + "hub/v1/dataevents/key:" + customerKey + "/rowset";

var request = new Script.Util.HttpRequest(endpoint);
    request.emptyContentHandling = 0;
    request.retries = 2;
    request.continueOnError = true;
    request.setHeader("Authorization", "Bearer " + accessToken);
    request.method = "POST";
    request.contentType = "application/json";
    request.encoding = "UTF-8";
    request.postData = Stringify(payload);

var results = request.send();

var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">

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

    var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
        accessToken     = "YOUR_REST_API_TOKEN";
	
	try {

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

        var payload = [
            {
                "keys": {
                    "SubscriberKey": GUID()
                },
                "values": {
                    "FirstName": "John",
                    "LastName": "Doe"
                }
            },
            {
                "keys": {
                    "SubscriberKey": GUID()
                },
                "values": {
                    "FirstName": "Jane",
                    "LastName": "Doe"
                }
            }
        ];

        var endpoint = restInstanceUrl + "hub/v1/dataevents/key:" + customerKey + "/rowset";

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "POST";
            request.contentType = "application/json";
            request.encoding = "UTF-8";
            request.postData = Stringify(payload);

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

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

</script>
[
    {
        "keys": {
            "subscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
        },
        "values": {
            "firstName": "John",
            "lastName": "Doe"
        }
    },
    {
        "keys": {
            "subscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
        },
        "values": {
            "firstName": "Jane",
            "lastName": "Doe"
        }
    }
]

TIP

Upserts multiple data extension rows. An upsert operation updates rows if the specified key exists and inserts new rows if the specified key doesn't exist.

Asynchronous

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

var payload = {
    items: [
        {
            SubscriberKey: GUID(),
            FirstName: "John",
            LastName: "Doe",
            EmailAddress: "example@mail.com"
        },
        {
            SubscriberKey: GUID(),
            FirstName: "Jane",
            LastName: "Doe",
            EmailAddress: "example@mail.com"
        }
    ]
}

var endpoint = restInstanceUrl + "data/v1/async/dataextensions/key:" + customerKey + "/rows";

var request = new Script.Util.HttpRequest(endpoint);
    request.emptyContentHandling = 0;
    request.retries = 2;
    request.continueOnError = true;
    request.setHeader("Authorization", "Bearer " + accessToken);
    request.method = "POST";
    request.contentType = "application/json";
    request.encoding = "UTF-8";
    request.postData = Stringify(payload);

var results = request.send();

var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">

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

    var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
        accessToken     = "YOUR_REST_API_TOKEN";
	
	try {

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

        var payload = {
            items: [
                {
                    SubscriberKey: GUID(),
                    FirstName: "John",
                    LastName: "Doe",
                    EmailAddress: "example@mail.com"
                },
                {
                    SubscriberKey: GUID(),
                    FirstName: "Jane",
                    LastName: "Doe",
                    EmailAddress: "example@mail.com"
                }
            ]
        }

        var endpoint = restInstanceUrl + "data/v1/async/dataextensions/key:" + customerKey + "/rows";

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "POST";
            request.contentType = "application/json";
            request.encoding = "UTF-8";
            request.postData = Stringify(payload);

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

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

</script>
{
  "requestId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
  "resultMessages": [
    
  ]
}

TIP

Send a PUT request in order to perform an Upsert action.

Reference

Ressources and references related to the current methods.

Official documentation
Data Extension Fields
Data Extensions Sync API
Data Extensions Async API
SOAP object

Last Updated: