top of page

Add/Update All Subscribers from Data Extension via SSJS

Updated: May 14, 2021

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.


For both the use cases you can add the below SSJS code in the cloud page and update as a one time activity or you can add the SSJS in script activity in automation. So that it updates regularly.


With help of the below code snippet of SSJS, you can add/update subscribers from Data extension (view code snippet).



Pre-Requisites:

  • 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:

Data Extension From Which All Subscribers list needs to be updated
Data Extension From Which All Subscribers list needs to be updated

Note:


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).


Out come:


  • 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.

Happy Learning!



10,020 views18 comments

18 commentaires


Sree Jangala
Sree Jangala
12 janv.

Hi Naveen, I need to update subscribers in SFMC all subscribers (parent and child BU) and need to verify updated in synchronized DE per list i have could you help me with that please?

J'aime

hunter.mb2022
28 juin 2022

Hi Naveen


I want to change the subscriber status from Held to Active via automation studio.


Can you help me with this?


J'aime
Naveen VM
Naveen VM
27 avr. 2023
En réponse à

Hi, you can't update the subscriber from Held to Active. You can delete the subscriber and re-add it.

J'aime

praful.d
08 juin 2022

Hi, Thank you for the above! But when I am running the code the subscribers are not getting pushed out from Data extension to publication list. Can you please help?

J'aime
Naveen VM
Naveen VM
27 avr. 2023
En réponse à

Hi, for publication list you have to pass the listid. Here is the reference link - https://gist.github.com/jdeblank/985d45db07c28679d84a612334ed9e7f

J'aime

murilo frederico
murilo frederico
01 févr. 2022

Hi, congratulations! Really good job, I would like to know if it's possible to move data from a list to a data extension


J'aime
murilo frederico
murilo frederico
01 févr. 2022
En réponse à

hmm I got it, but I need do it using ssjs, to use in my web page, it's possible? but thanks very much!

J'aime

Goku
Goku
22 juin 2021

I am trying to change the Status 'Active' to 'Unsubscribe' as you said to use Unsubscribe Method but nothing happened. It shows this error "A runtime error occurred while the SSJS Activity executed the SSJS script. Please review the SSJS activity to ensure that errors are handled properly." Please let me know what to do in this case.

J'aime
Naveen VM
Naveen VM
25 juin 2021
En réponse à

You can use this blog - https://www.salesforcefan.com/post/sending-triggered-email-via-ampscript but you need to customise a bit according to your use case.

J'aime
bottom of page