0

The below code was working perfectly but don't know what happened the code is not working now. When I'm clicking the unsubscribe link it is not updating the values.

    %%[
    var @jid
    var @listid
    var @batchid
    var @email
    var @skey
    set @jid =""
    set @listid = ""
    set @batchid = ""
    set @email = AttributeValue("emailaddr")
    set @skey = AttributeValue("_subscriberkey")
    ]%%
    <a href="%%=CloudPagesUrl(1148,'JobID',@jid,'ListID',@listid,'BatchId',@batchid,'Email',@email,'SubKey',@skey)=%%">Unsubscribe</a>

HandlerCode

%%[

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

set @debug = 0
set @jid = RequestParameter("JobID")
set @listid = RequestParameter("ListID")
set @batchid = RequestParameter("BatchId")
set @email = RequestParameter("Email")
set @skey = RequestParameter("SubKey")
set @reason = "One-Click Unsubscribe"
set @unsubscribeAll = RequestParameter("ua")
set @check = RequestParameter("sure")



/* 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

]%%
3
  • Was Web Analytics connector configured on this account recently? You are outputing your link with just %%=CloudPagesURL()=%% and adding WAC to it will break the URL parameters. Does changing your href to <a href="%%=RedirectTo(CloudPagesUrl(1148,'JobID',@jid,'ListID',@listid,'BatchId',@batchid,'Email',@email,'SubKey',@skey))=%%">Unsubscribe</a> fixes it? – Mateusz Bartkowiak Oct 23 '19 at 13:31
  • No still not working – Ankur Nigam Oct 23 '19 at 14:01
  • @MateuszBartkowiak the values are not getting passed through CloudPagesUrl, I tried debugging the the values are null – Ankur Nigam Oct 23 '19 at 15:15
2

There is no need to add parameters to your CloudPagesUrl function. It passes automatically information about the email and the subscriber in an encrypted string.

Use this function in an email to pass information via a URL in an encrypted query string. For example, you could share a single unsubscription or profile center page for use in any sent email message. This method passes information in an encrypted query string without passing subscriber information or values in clear text.

Reference

https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/cloudpagesurl.htm

You button's code should be:

<button class="button button1" type="submit" name="sendNewSms" id="sendNewSms" value=" Send " onclick="location.href = '%%=CloudPagesURL(3489)=%%'">UNSUBSCRIBE</button>

On the other side, use this AttributeValue instead of RequestParameter:

    SET @emailaddr = AttributeValue("emailaddr")
    SET @emailName = AttributeValue("emailname_") 
    SET @subKey = AttributeValue("_subscriberkey")
    SET @jid = AttributeValue("jobid")
    SET @batchid = AttributeValue("_JobSubscriberBatchID")

Also, I see that you are passing the ListId as a parameter. Unless you have multiple lists, I suggest that you hard code it on the second part of your code.

8
  • I tried but still nothing happened. This is strange because it was working but dont know what happened – Ankur Nigam Oct 23 '19 at 13:42
  • In which part are you setting the ListID value? I don't see it in your code.. – Rachid Mamai Oct 23 '19 at 13:44
  • I am passing it as blank because I want to unsubscribe the user from ALL Subscribers list – Ankur Nigam Oct 23 '19 at 13:46
  • Can you describe how are you testing the unsub? – Rachid Mamai Oct 23 '19 at 13:49
  • I am sending a mail from a list and on that mail there is an unsubscribe link when the user clicks that link he will get a page where there is an unsubscribe button in which that CloudPageURL code is written when the user clicks that it goes to that handler page – Ankur Nigam Oct 23 '19 at 14:04
0

I got the solution. Actually the thing was that I am using 1 email through which the user can access the unsubscribe cloud page. This cloud page contains the button for unsubscribing which further goes to the handler cloud page. I was doing everything right but the problem was in the email. Instead of providing the %%=CloudPagesURL()=%% function we were directly pasting the cloud page url due to which the user details were not passed, thus all the values were null and no one was getting unsubscribed. When I replaced the URL everything was working perfectly.

2
  • Isn’t that what Rachid suggested, that you should change the code in your unsubscribe button? – zuzannamj Oct 23 '19 at 19:16
  • yeah i've voted for his answer..but there was no problem in the button the parameters were required to be passed there was no problem in that. The problem was in the link that redirects to the page that contained this button. But yeah the logic he gave solved the problem – Ankur Nigam Oct 24 '19 at 5:35

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.