Add/Update All Subscribers from Data Extension via SSJS
Updated: Aug 4, 2020
Have you ever come across a scenario, where if a contact / lead changes its email address in Salesforce and in Marketing cloud the same contact / lead email address gets changed in synchronised data extension and normal data extension if you are using update / overwrite action while doing a SQL query. But it wouldn't have changed in the All - subscriber list. This will result in sending an email to the old contact email address.
Or let's say you have added a new profile attribute called 'First name' and you would need to populate that value to all subscriber lists from a master data extension.
With help of the below code snippet of SSJS, you can add/update subscribers from Data extension (view code snippet).
Create a data extension. In the above example the data extension name is 'Test_AddSubscribers'
'IsSubscriber' is one of the columns in the data extension where it shows the subscriber has been added for new subscriber and updated for existing subscribers.
For demo purposes, I have kept the subscriber key as an email address, Change the Line No - 27 for changing the subscriber key to be added in all subscriber lists.
Below is the properties for the data extension:
This will only retrieve and update / insert All subscribers up-to 2000 records at once. If you want to retrieve more than 2000 records, then you would need to tweak the code with pagination. In this way by running the same script you can retrieve the records from data extension and update/insert the all subscriber list.
Also, if you are planning to change the status from Active to unsubscribe then you would need to call the 'Unsubscribe' method instead of 'update' method.
But, consider SSJS will take so much time to update if you have so many records. The easiest way would be using Automation studio with Data Extract Activity (extract the data extension records in a csv format) > File Transfer (move the extracted record from safe house to marketing cloud SFTP) > Import (Import the file from SFTP to All subscribers list).
If you just want to update but not to insert then remove the else part from the SSJS snippet.
If the email address is not present in all subscriber list it will add it in All - subscriber list and will update the data extension row as 'ok added'.
If the email address has matched then it will update the All subscriber list and will update the data extension row as 'ok updated'.
Please feel free to leave your feedback/comments in case of any queries.