1

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"):

SSJS

<script runat=server>
  Platform.Load("core","1");
  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);
  Write(status);
</script>

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?

3

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

<script runat=server>
  Platform.Load("core","1");
  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);
  Write(status);
</script>

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">
    Platform.Load("core","1.1.5");
    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);
    Write(data.Results[0].StatusCode);
</script>
2
  • 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.