I'm attempting to update the Email Address of a specific subscriber using SSJS.
The SSJS is retrieving the Subscriber Key and New Value (updated email address) in a Data Extension. If the Subscriber Key matches a subscriber key in the All Subscribers table, it should replace the email address of that record with the email address in the New Value column in the Data Extension.
I'm running the script, but no changes are occurring and I'm not getting any errors. Below is the code I'm using.
<script runat="server">
Platform.Load("Core", "1.1.1");
try {
var dataExtensionKey = "ReplaceExternalKey";
var dataExtension = DataExtension.Init(dataExtensionKey);
var rows = dataExtension.Rows.Retrieve();
var subscribersToUpdate = [];
if (rows.length > 0) {
for (var i = 0; i < rows.length; i++) {
var subscriberKey = rows[i]["18-digit Contact Id"];
var newEmailAddress = rows[i]["New Value"];
// Add subscriber to the array
subscribersToUpdate.push({
"EmailAddress": newEmailAddress,
"SubscriberKey": subscriberKey
});
}
// Update the All Subscribers List with the new email addresses
var status = Subscriber.Update(subscribersToUpdate);
// Check the status for each subscriber
for (var j = 0; j < status.length; j++) {
if (status[j].Status == "OK") {
Write("Subscriber with SubscriberKey " + subscribersToUpdate[j].SubscriberKey + " was updated successfully.<br>");
} else {
Write("Failed to update Subscriber with SubscriberKey " + subscribersToUpdate[j].SubscriberKey + ".<br>");
}
}
} else {
Write("No rows found in the data extension.<br>");
}
} catch (e) {
Write("An error occurred: " + Stringify(e) + "<br>");
}
</script>