0

Let's say that I had created an unsubscribe dynamic Cloudpage:

  1. Different emails redirect to this cloudpage.
  2. Each email is related to a specific market (identified by a marketID).
  3. The cloudpage has to update the consent of each related market. Depending on the email, the cloud page displays a checked box related to a specific market optin in Salesforce CRM.
  4. In salesforce CRM, each contact is linked to a consent object. In this consent object, there is 13 optin checkbox which shows the choice of the contact. Each optin is related to a market.

Expected behavior: When the contact click on the unsuscribe link in the email, the cloudpage appears. This cloudpage displays a checked box on the specific market and when the contact submit the page :

  • The optin related to the market is updated to unchecked on the consent object in Salesforce CRM.

  • The others optin related to others markets do not have to change if the contact doesn’t not click on the other unsubscribe links from the other emails. Only the optin related to the market submitted on the cloudpage has to change.

Actual Behavior: When the contact submit the cloudpage, the specific market is unckecked but the other optins are checked automatically.

Here is the code that I have created :

<table cellpadding="0" cellspacing="0" width="100%" role="presentation" style="min-width: 100%; " class="stylingblock-content-wrapper"><tr><td class="stylingblock-content-wrapper camarker-inner"><script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>%%[
var @MarketId, @contactId, @consentId, @updateRecord

/*retrieve values from email*/
SET @MarketId = RequestParameter('MarketId')
SET @contactId = _subscriberkey

/* fetch data from Sales Cloud to show in the form */
SET @subscriberRows = RetrieveSalesforceObjects("Consent__c","Id,OptIn_Email_Market1__c,OptIn_Email_Market2__c, OptIn_Email_Market3, OptIn_Email_Market4","Contact__c", "=", @contactId)

IF RowCount(@subscriberRows) == 1 THEN
SET @row = row(@subscriberRows,1)
SET @ConsentId = field(@row,"Id")
ENDIF


IF RequestParameter("submitted") == true THEN
/* update Consent */
SET @updateRecord = UpdateSingleSalesforceObject(
"Consent__c", RequestParameter('cid'),
"OptIn_Email_Market1__c", Iif(RequestParameter("EmailMarket1") == "on", "false", "true"),
"OptIn_Email_Market2__c", Iif(RequestParameter("EmailMarket2") == "on", "false", "true"),
"OptIn_Email_Market3__c", Iif(RequestParameter("EmailMarket3") == "on", "false", "true"),
"OptIn_Email_Market4__c", Iif(RequestParameter("EmailMarket4") == "on", "false", "true"))
ENDIF]%%
<script runat="server">
}catch(e){
Write(Stringify(e));
}
</script>
<style>
ul li{
display: inline-block;
list-style: none
}
.alignClass{
text-align:center
}
.isCursor{
cursor:pointer;
}
.buttonClass{
background-color: #05425F;
border-radius: 24px;
border:none;
padding:8px;
cursor:pointer;
color: white;
text-align:center;
}
</style>
<div style="padding-left:20px">
<form action="%%=RequestParameter('PAGEURL')=%%" method="post">
%%[IF @MarketId == "a116700001KM15XAAT" then ]%%
<div>
       <input type="checkbox" class="isCursor" name="EmailMarket1" checked="">
       <label>I would like to unsusbcribe from the Market 1 communications </label>
</div>
 %%[ELSEIF @MarketId == "b116700001KM15bBBD" then ]%%
<div>
        <input type="checkbox" class="isCursor" name=" EmailMarket2" checked="">
        <label> I would like to unsusbcribe from the Market 2 communications </label>
</div>
%%[ELSEIF @MarketId == "b116700001KM15iAAD" then ]%%
<div>
        <input type="checkbox" class="isCursor" name=" EmailMarket3" checked="">
        <label> I would like to unsusbcribe from the Market 3 communications </label>
</div>
%%[ELSEIF @MarketId == "b116700001KM15fAAD" then ]%%
<div>
        <input type="checkbox" class="isCursor" name=" EmailMarket4" checked="">
        <label> I would like to unsusbcribe from the Market 3 communications </label>
</div>

%%[ ENDIF ]%%
<input name="submitted" type="hidden" value="true"><br>
<input name="cid" type="hidden" value="%%=v(@ConsentId)=%%"><br>
<div style="display:flex;align-items:center">
<input type="submit" value="Submit my preferences" class="buttonClass">
%%[ if @updateRecord == 1 then ]%%
<p style="color:#639e30;margin-left:15px">Your preferences has been registered.</p>
%%[ elseif @updateRecord == 0 then ]%%
<p style="color:#f7aa36;margin-left:15px">Error on the update, Try again.</p> 
%%[ 
endif 
]%%
</div></form></div>

Thanks for your help

1 Answer 1

1

I suspect you're not setting @MarketID on POST. You can set that as a hidden input like cid.

Debugging output is your friend. I like to explicitly distinguish between GET and POST.

<script runat="server">

Platform.Load("Core","1");

try {

</script>
%%[

    set @debug = 1

    set @submitted = RequestParameter("submitted")
    set @action = iif(empty(@submitted),"GET","POST")
    
    if @debug == 1 then
      output(concat("<br>action: ", @action))
    endif

    /* GET */
    if @action == "GET" then 

      /* retrieve values from email*/
      SET @MarketId = RequestParameter('MarketId')
      SET @contactId = AttributeValue("_subscriberkey")
      
      /* fetch data from Sales Cloud to show in the form */
      SET @subscriberRows = RetrieveSalesforceObjects("Consent__c","Id,OptIn_Email_Market1__c,OptIn_Email_Market2__c,OptIn_Email_Market3,OptIn_Email_Market4","Contact__c", "=", @contactId)

      set @rowCount = rowCount(@subscriberRows)

      IF @rowCount == 1 THEN
        SET @row = row(@subscriberRows,1)
        SET @ConsentId = field(@row,"Id")
      ENDIF

      if @debug == 1 then
        output(concat("<br>GET"))
        output(concat("<br>MarketId: ", @MarketId))
        output(concat("<br>contactId: ", @contactId))
        output(concat("<br>rowCount: ", @rowCount))
        output(concat("<br>ConsentId: ", @ConsentId))
      endif


    /* POST */
    else

      set @cid = RequestParameter('cid')
      SET @MarketId = RequestParameter('MarketId')
      set @EmailMarket1 = RequestParameter("EmailMarket1")
      set @EmailMarket2 = RequestParameter("EmailMarket2")
      set @EmailMarket3 = RequestParameter("EmailMarket3")
      set @EmailMarket4 = RequestParameter("EmailMarket4")

      /* update Consent */
      SET @updateRecord = UpdateSingleSalesforceObject(
        "Consent__c", @cid,
        "OptIn_Email_Market1__c", Iif(@EmailMarket1 == "on", "false", "true"),
        "OptIn_Email_Market2__c", Iif(@EmailMarket2 == "on", "false", "true"),
        "OptIn_Email_Market3__c", Iif(@EmailMarket3 == "on", "false", "true"),
        "OptIn_Email_Market4__c", Iif(@EmailMarket4 == "on", "false", "true")
      )

      if @debug == 1 then
        output(concat("<br>POST"))
        output(concat("<br>cid: ", @cid))
        output(concat("<br>EmailMarket1: ", @EmailMarket1))
        output(concat("<br>EmailMarket2: ", @EmailMarket2))
        output(concat("<br>EmailMarket3: ", @EmailMarket3))
        output(concat("<br>EmailMarket4: ", @EmailMarket4))
        output(concat("<br>updateRecord: ", @updateRecord))
      endif   

    ENDIF
]%%
<script runat="server">

} catch(e) {

   Write("<br>e: " + Stringify(e));
}

</script>

%%[ if @action == "GET" then ]%%

  <form action="%%=RequestParameter('PAGEURL')=%%" method="post">

    %%[ IF @MarketId == "a116700001KM15XAAT" then ]%%

      <div>
        <input type="checkbox" name="EmailMarket1" checked="">
        <label>I would like to unsubscribe from the Market 1 communications</label>
      </div>

     %%[ ELSEIF @MarketId == "b116700001KM15bBBD" then ]%%
     
      <div>
        <input type="checkbox" name="EmailMarket2" checked="">
        <label> I would like to unsubscribe from the Market 2 communications</label>
      </div>

    %%[ ELSEIF @MarketId == "b116700001KM15iAAD" then ]%%

      <div>
        <input type="checkbox" name="EmailMarket3" checked="">
        <label> I would like to unsubscribe from the Market 3 communications</label>
      </div>

    %%[ ELSEIF @MarketId == "b116700001KM15fAAD" then ]%%

      <div>
         <input type="checkbox" name="EmailMarket4" checked="">
         <label> I would like to unsubscribe from the Market 3 communications</label>
      </div>

    %%[ ENDIF ]%%

    <input name="submitted" type="hidden" value="true">
    <input name="cid" type="hidden" value="%%=v(@ConsentId)=%%">
    <input name="MarketId" type="hidden" value="%%=v(@MarketId)=%%">
    <br><input type="submit" value="Submit my preferences">

  </form>

%%[ elseif @action == "POST" then ]%%

  %%[ if @updateRecord == 1 then ]%%

    <p style="color:#639e30;margin-left:15px">Your preferences has been registered.</p>

  %%[ else ]%%

  <p style="color:#f7aa36;margin-left:15px">Error on the update, Try again.</p> 

  %%[ endif ]%%

%%[ endif ]%%
3
  • Hi @Adam Spriggs, I have corrected my page with the code you send me but I can't publish it I do have an error : "An error occurred while previewing this content. This can happen for many reasons, including an incomplete or incorrect MC script (AMPscript, SSJS, or GTL) or missing subscriber context. Click Cancel to review your code or Publish to send the updated content live." Commented Aug 25, 2022 at 13:25
  • I'd suggest creating a new page with my code and sending yourself an email that targets it with a link using CloudPagesURL. Once you've got it working, then you can update your existing page. The page is dependent on _subscriberkey. Commented Aug 25, 2022 at 16:07
  • Hi @Adam Spriggs, I had follow your advice and it worked ! The market ID is missing on the object that I request as you supected. Commented Aug 26, 2022 at 11:02

You must log in to answer this question.

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