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": "[email protected]",
    "SubscriberKey": "[email protected]",
    "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?

1 Answer 1

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: "[email protected]",
    SubscriberKey: "[email protected]",
    EmailTypePreference: "HTML",
    Attributes:{ "First Name": "test.1", "Last Name": "test.1" },
    Lists: {"Status": "Active", "ID": 12345, "Action": "Upsert"}
  };
  var subObj = Subscriber.Init("[email protected]");
  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: '[email protected]',
        EmailAddress: '[email protected]',
        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
    Commented Sep 24, 2020 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. Commented Sep 26, 2020 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .