verified
Update a Data Extension recordLearn how to update (upsert) 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: "Customers",
filter: {
Names: [
"SubscriberKey",
"EmailAddress"
],
Values: [
"S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"example@mail.com"
]
},
attributes: {
Names: [
"FirstName",
"LastName"
],
Values: [
"John",
"Doe"
]
}
}
var result = Platform.Function.UpdateData(
config.dataextension,
config.filter.Names,
config.filter.Values,
config.attributes.Names,
config.attributes.Values
);
Platform.Response.Write(Platform.Function.Stringify(result));
<script runat="server">
try {
var config = {
dataextension: "Customers",
filter: {
Names: [
"SubscriberKey",
"EmailAddress"
],
Values: [
"S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"example@mail.com"
]
},
attributes: {
Names: [
"FirstName",
"LastName"
],
Values: [
"John",
"Doe"
]
}
}
var result = Platform.Function.UpdateData(
config.dataextension,
config.filter.Names,
config.filter.Values,
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 = {
FirstName : "John",
LastName : "Doe"
};
var result = de.Rows.Update(
payload,
["SubscriberKey", "EmailAddress"],
["S0M3-GU1D-K3Y-G03SR1G4T-H3R3", "example@mail.com"]
);
<script runat="server">
Platform.Load("core", "1");
try {
var de = DataExtension.Init("MyDataExtension");
var payload = {
FirstName : "John",
LastName : "Doe"
};
var result = de.Rows.Update(
payload,
["SubscriberKey", "EmailAddress"],
["S0M3-GU1D-K3Y-G03SR1G4T-H3R3", "example@mail.com"]
);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
1
WSProxy
var api = new Script.Util.WSProxy();
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var props = [
{
"Name": "SubscriberKey",
"Value": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
},
{
"Name": "EmailAddress",
"Value": "example@mail.com"
}
];
var result = api.updateItem('DataExtensionObject', {
CustomerKey: customerKey,
Properties: props
});
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var props = [
{
"Name": "SubscriberKey",
"Value": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
},
{
"Name": "EmailAddress",
"Value": "example@mail.com"
}
];
var result = api.updateItem('DataExtensionObject', {
CustomerKey: customerKey,
Properties: props
});
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"Status": "OK",
"RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Results": [
{
"ErrorMessage": null,
"KeyErrors": null,
"ValueErrors": null,
"Object": {
"Name": null,
"Keys": null,
"Type": null,
"Properties": [
{
"Name": "SubscriberKey",
"Value": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
},
{
"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
},
"UpdateResults": null,
"ParentPropertyName": null,
"StatusCode": "OK",
"StatusMessage": "Updated 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: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
FirstName: "John",
LastName: "Doe",
EmailAddress: "example@mail.com"
},
{
SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
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 = "PUT";
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: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
FirstName: "John",
LastName: "Doe",
EmailAddress: "example@mail.com"
},
{
SubscriberKey: "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
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 = "PUT";
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": [
]
}
Reference
Ressources and references related to the current methods.
Official documentation
SOAP object
Data Extensions Sync API
Data Extensions Async API