0

I'm trying to do it this way, but I'm getting 500 error

%%[

var @SubscriberKey, @ListID, @ListType

set @SubscriberKey = "example"
set @ListID= 123 
SET @ListType= "Publication List"

IF Not Empty(@SubscriberKey) THEN

set @UpsertData = UpsertData("_ListSubscribers",3,"SubscriberKey", @SubscriberKey, "ListID", @ListID, "ListType", @ListType, "Status", "Active")
OUTPUTLINE(CONCAT("<br>Rows updated: ",@UpsertData))

ENDIF

]%%
  • You can't update dataviews, you would need to make an API call to update this – EazyE Jul 9 at 19:48
1

If you are looking to just change status for subscriber inside of a list, you will need to use SFMC SSJS Core functions or WSProxy. You can use the AMPScript API functions, but this is highly inefficient and requires a ton of unnecessary processing.

For Core, you would look at using List.Subscribers:

For making a user active, you would want to do List.Subscribers.Update like so:

var myList = List.Init("myList");
var status = myList.Subscribers.Update("mySubscriberKey","Active");

For unsubscribing, you can use the Core function List.Subscribers.Unsubscribe like so:

var myList = List.Init("myList");
var status = myList.Subscribers.Unsubscribe("mySubscriberKey");

One note on this is that if you are trying to create an unsub event (tying the unsubscribe to a job with reason, etc.) then this is not your preferred path. You will want to use the 'LogUnsubEvent' API call. This is best accomplished via WSProxy:

<script runat="server">
try {
    var prox = new Script.Util.WSProxy();

    /* Set ClientID */
    prox.setClientId({ "ID": mid}); //Impersonates the BU

    var props = [
       { Name: "SubscriberKey", Value: "sample@sample.com" },
       { Name: "EmailAddress", Value: "sample@sample.com" },
       { Name: "JobID", Value: 18099 },
       { Name: "ListID", Value: 8675309 },
       { Name: "BatchID", Value: 0 }
    ];
    var data = prox.execute(props, "LogUnsubEvent");
    
    Platform.Response.Write(Platform.Function.Stringify(data));
} catch(e) {
    Platform.Response.Write(Platform.Function.Stringify(e));
}
</script>

Here is an article I wrote about doing it.

That being said, if you are looking to do the changes in bulk, then WSProxy is the better way to go as it has batch options.

<script runat="server">    

  var prox = new Script.Util.WSProxy();
  var subArr = [];
  var myArr = //my Array containing the sub info needed to be changed
 
  for(i=1;i<myArr;i++) {
    //iterate through your 'myArr' to grab the correct info and insert into below
    var subObj = {
        SubscriberKey: subkey,
        EmailAddress: email,
        Lists: [{
            ID: '12345',
            Status: 'Active'
        }]
    };
    subArr.push(subObj);
  }
 
  var options = { 
    SaveOptions: [{
      PropertyName: "*",
      SaveAction: "UpdateAdd"
    }]
  };

 var resp = prox.createBatch("Subscriber", subArr, options);
</script>

If you desperately want to do it via AMPScript API functionality, I would look at this article by Zuzanna that details out the LogUnsubEvent calls for you in both AMPScript and SSJS.

| improve this answer | |
  • 1
    Wow... Thanks Gregory for sharing the detailed knowledge on possibilities of list updation along with when to use them... Very helpful – Vishal Kumar C V Jul 11 at 5:00
0

There are many ways you can achieve this. But due to the simplicity and I prefer to use WSProxy method as below.

<script runat="server">
Platform.Load("core","1.1.5"); 

//Making use of Web-socket Proxy
var prox = new Script.Util.WSProxy();
var options = {SaveOptions: [{'PropertyName': '*', SaveAction: 'UpdateAdd'}]};

//Similar to RequestParameter in AMPscript to get values from Form submitted
var sub_key = Platform.Request.GetFormField('subkey')
var email = Platform.Request.GetFormField('email')

function unsub() {
prox.setClientId({ "ID": "10966194" }); //Setting MID
var props = { 
                SubscriberKey: sub_key,
                EmailAddress: email,
                Lists: [{ID: '1753',    //Setting your LIST_ID
                        Status: 'Unsubscribed' // to unsub = "Unsubscribed" to subscribe = "Active"
                        }]
            };
var data = prox.updateItem("Subscriber", props, options);

return data.Results[0].StatusCode;
}

unsub(); //Run the above custom function
</script>


Received value subkey = <ctrl:var name=sub_key /> <br/><br/>
Hi User, your email address <ctrl:var name=email /> has been unsubscribed.
| improve this answer | |

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.