We were requested to create a dynamic subscription/unsubscription center using Cloudpages on Marketing Cloud.

We're trying to implement this using SSJS, but code does not work (throws "Error"):


<script runat=server>
  var newSubscriber = {
    "EmailAddress": "test@example.com",
    "SubscriberKey": "test@example.com",
    "EmailTypePreference": "Text",
    "Attributes":{ "First Name": "test.1", "Last Name": "test.1" },
    "Lists": {"Status": "Active", "ID": "1****8", "Action": "Update"}
  var status = Subscriber.Upsert(newSubscriber);

We tried both lists and publication lists (being the second one our main goal).

What's wrong with this code? How do I fix it?


As shown in the documentation you need to call the init function on the Subscriber object first.

<script runat=server>
  var newSubscriber = {
    EmailAddress: "test@example.com",
    SubscriberKey: "test@example.com",
    EmailTypePreference: "HTML",
    Attributes:{ "First Name": "test.1", "Last Name": "test.1" },
    Lists: {"Status": "Active", "ID": 12345, "Action": "Upsert"}
  var subObj = Subscriber.Init("test@example.com");
  var status = subObj.Upsert(newSubscriber);

I tested the above and it upserts to a publist successfully. I actually find that using WSProxy much more flexible and simpler to use, but it's personal preference. Here's an example:

<script runat="server">
    var prox = new Script.Util.WSProxy();
    var options = {SaveOptions: [{'PropertyName': '*', SaveAction: 'UpdateAdd'}]};

    var props = { 
        SubscriberKey: 'test@email.com',
        EmailAddress: 'test@email.com',
        Lists: [{
            ID: 12345,
            Status: 'Active'
    var data = prox.updateItem('Subscriber', props, options);
  • How do I do the same using DE. So If I have email address in DE using the above SSJS how do I achieve the same task?
    – Sohail
    Sep 24 '20 at 1:41
  • Use a Lookup function for that (or Rows.Retrieve). Hopefully you have both email and subscriber key in your DE. Then use those variable values in place of the static values in the example above. Sep 26 '20 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.