1

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>

1 Answer 1

1

You would need to initialize the subscriber first, then you can use the update method to update the subscriber object

References:

Based on your code above, you can try something like this:

<script runat="server">
Platform.Load("Core", "1.1.1");


var dataExtensionKey = "ReplaceExternalKey";
var dataExtension = DataExtension.Init(dataExtensionKey);
var rows = dataExtension.Rows.Retrieve();

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"];
        var attribute =  {EmailAddress: newEmailAddress};

        var subObj = Subscriber.Init(subscriberKey);        
        var status = subObj.Update(attribute);
    }

} else {
    Write("No rows found in the data extension.<br>");
}

</script>

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.