4

I created a one-click unsubscribe. But it is not unsubscribing any user.The user is still active in all subscriber list. I've used the below code


%%[
/* declare variables */
var @sid, @jid, @listid, @batchid, @reason, @lue, @lue_prop, @lue_statusCode, @overallStatus, @requestId, @Response, @Status, @Error

/* import the personalization string values from the query string into the variables */
set @skey = AttributeValue("_subscriberkey")
set @jobid = AttributeValue("jobid")
set @listid = AttributeValue("listid")
set @batchid = AttributeValue("_JobSubscriberBatchID")

/* set the reason for the unsubscribe */
set @reason = "Custom Unsubscribe"

/* initiate the LogUnsubEvent request */
set @lue = CreateObject("ExecuteRequest")
SetObjectProperty(@lue, "Name", "LogUnsubEvent")

/* configure the properties of the API object */
set @lue_prop = CreateObject("APIProperty")
SetObjectProperty(@lue_prop, "Name", "SubscriberKey")
SetObjectProperty(@lue_prop, "Value", @skey)
AddObjectArrayItem(@lue, "Parameters", @lue_prop)

set @lue_prop = CreateObject("APIProperty")
SetObjectProperty(@lue_prop, "Name", "JobID")
SetObjectProperty(@lue_prop, "Value", @jobid)
AddObjectArrayItem(@lue, "Parameters", @lue_prop)

set @lue_prop = CreateObject("APIProperty")
SetObjectProperty(@lue_prop, "Name", "ListID")
SetObjectProperty(@lue_prop, "Value", @listid)
AddObjectArrayItem(@lue, "Parameters", @lue_prop)

set @lue_prop = CreateObject("APIProperty")
SetObjectProperty(@lue_prop, "Name", "BatchID")
SetObjectProperty(@lue_prop, "Value", @batchid)
AddObjectArrayItem(@lue, "Parameters", @lue_prop)

set @lue_prop = CreateObject("APIProperty")
SetObjectProperty(@lue_prop, "Name", "Reason")
SetObjectProperty(@lue_prop, "Value", @reason)
AddObjectArrayItem(@lue, "Parameters", @lue_prop)

/* you have to set the ClientID only when working with On Your Behalf accounts
var @mid, @lue_oyb
set @mid = memberid
set @lue_oyb = CreateObject('ClientID')
SetObjectProperty(@lue_oyb, 'ID', @mid)
SetObjectProperty(@lue, 'Client', @lue_oyb)
*/

/* this is where the unsubscribe is performed */
set @lue_statusCode = InvokeExecute(@lue, @overallStatus, @requestId)
/*
set @Response = Row(@lue_statusCode, 1)
set @Status = Field(@Response, "StatusMessage")
set @Error = Field(@Response, "ErrorCode")

IF (@Error == "12012") OR (@Error == "401") OR (@Status == "Event posted") THEN
     /* it worked */
 ELSE
 */
     /* it failed, do a normal update */
     SET @sub = CreateObject("Subscriber")
     SetObjectProperty(@sub, "SubscriberKey", @skey)

     SET @list = CreateObject("SubscriberList")
     SetObjectProperty(@list, "Status", "Unsubscribed")
     SetObjectProperty(@list, "ID", @listid)
     SetObjectProperty(@list, "Action", "Update")
     AddObjectArrayItem(@sub, "Lists", @list)
     SET @statusCode = InvokeUpdate(@sub, @statusMsg, @errorCode) 
 ENDIF

]%%

1 Answer 1

10

You're not checking if the variables have been populated, your code is missing if not empty rules. I suggest trying with the following script in the original version, as it was posted by Adam on his blog:

%%[

var @debug
var @jid
var @listid
var @batchid
var @email
var @skey
var @reason
var @unsubscribeAll

set @debug = 0
set @jid = AttributeValue("jobid")
set @listid = AttributeValue("listid")
set @batchid = AttributeValue("_JobSubscriberBatchID")
set @email = AttributeValue("emailaddr")
set @skey = AttributeValue("_subscriberkey")
set @reason = "One-Click Unsubscribe"
set @unsubscribeAll = RequestParameter("ua")

/* if we know the subscriber */
if not empty(@skey) then

   var @lue
   var @lue_prop
   var @lue_statusCode
   var @overallStatus
   var @requestId
   var @Response
   var @Status
   var @Error

   /* if unsubscribing from all, then set the job, batch and listids to blank, effectively doing a global unsub */
   if @unsubscribeAll == "1" then
     set @jid = ""
     set @listid = ""
     set @batchid = ""
   endif

   /* create a request to inject an unsub event into the LogUnsubEvent platform table */
   set @lue = CreateObject("ExecuteRequest")
   SetObjectProperty(@lue,"Name","LogUnsubEvent")

   /*
    In order to invoke the request, we need to associate the following information with it to define the subscriber context and the job context:

    1. Subscriber Key
    2. JobId associated with the email send
    3. ListID the email was sent to
    4. BatchID the email was sent to
    5. Reason for the unsub
   */

   /* 1. define and associate Subscriber Key to the request */
   set @lue_prop = CreateObject("APIProperty")
   SetObjectProperty(@lue_prop, "Name", "SubscriberKey")
   SetObjectProperty(@lue_prop, "Value", @skey)
   AddObjectArrayItem(@lue, "Parameters", @lue_prop)

   /* 2. define and associate JobID to the request */
   if not empty(@jid) then
    set @lue_prop = CreateObject("APIProperty")
    SetObjectProperty(@lue_prop, "Name", "JobID")
    SetObjectProperty(@lue_prop, "Value", @jid)
    AddObjectArrayItem(@lue, "Parameters", @lue_prop)
   endif

   /* 3. define and associate ListID to the request */
   if not empty(@listid) then
     set @lue_prop = CreateObject("APIProperty")
     SetObjectProperty(@lue_prop, "Name", "ListID")
     SetObjectProperty(@lue_prop, "Value", @listid)
     AddObjectArrayItem(@lue, "Parameters", @lue_prop)
   endif

   /* 4. define and associate BatchID to the request */
   if not empty(@batchid) then
    set @lue_prop = CreateObject("APIProperty")
    SetObjectProperty(@lue_prop, "Name", "BatchID")
    SetObjectProperty(@lue_prop, "Value", @batchid)
    AddObjectArrayItem(@lue, "Parameters", @lue_prop)
   endif

   /* 5. define and associate unsub reason to the request */
   set @lue_prop = CreateObject("APIProperty")
   SetObjectProperty(@lue_prop, "Name", "Reason")
   SetObjectProperty(@lue_prop, "Value", @reason)
   AddObjectArrayItem(@lue, "Parameters", @lue_prop)

   /* finally, you invoke the request */
   set @lue_statusCode = InvokeExecute(@lue, @overallStatus, @requestId)

   /* extract messages from the response */
   set @Response = Row(@lue_statusCode, 1)
   set @Status = Field(@Response,"StatusMessage")
   set @Error = Field(@Response,"ErrorCode")

endif

if @debug == 1 then
  output(concat("<br>jid: ", @jid))
  output(concat("<br>listid: ", @listid))
  output(concat("<br>batchid: ", @batchid))
  output(concat("<br>email: ", @email))
  output(concat("<br>skey: ", @skey))
  output(concat("<br>reason: ", @reason))
  output(concat("<br>unsubscribeAll: ", @unsubscribeAll))
  output(concat("<br>overallStatus: ", @overallStatus))
  output(concat("<br>requestId: ", @requestId))
  output(concat("<br>Response: ", @Response))
  output(concat("<br>Status: ", @Status))
  output(concat("<br>Error: ", @Error))
endif

]%%
2
  • 3
    Also, unsub events are not always immediate. I've gotten tripped up by that in testing. Debugging output helps in testing, also. Commented Sep 20, 2019 at 14:39
  • It worked. Thanks! Commented Sep 21, 2019 at 9:55

You must log in to answer this question.

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