crmTech/ May 30, 2019/ Cloud, CRM, Salesforce Marketing Cloud/ 0 comments

If Subscriber Key and EmailAddress is not same then you can use the below code snippet to unsubscribe.

Input for below code snippet

  1. PubId: External Id of Publication list which will get passed from Email in Unsubscribe link.
  2. emailId : email address which will get passed from Email in Unsubscribe link.
<html>
 <head>
   <body>
    <!-- Start: Added to Fetch the Subscriber Email Address -->
    %%[
    SET @emailId = QueryParameter("email")
    SET @PubId = QueryParameter("PubId")
    ]%%
    <!--p> Your Email Address is %%=v(@emailaddr)=%%</p-->
    <!-- End:Added to Fetch the Subscriber Email Address -->
	<script runat=server>
	/***************************************************************************
	Purpose : Unsubscribe from the All Publication and All Subscribers List. 
	PubId is nothing but the External Id of Publication list which will get passed from Email in Unsubscribe link.
	***************************************************************************/
	 /*UNSUBSCRIBE ALL CODE START*/
	 Platform.Load("Core","1");
	 var emailId = Platform.Variable.GetValue('@emailId'); 
	 var listIdArray,listId,listName,listKeylists,myList,status;
	 var unSubList = ""; 
	 var pubId = Platform.Variable.GetValue('@PubId');
	 var data = DataExtension.Init("SSJS_Preferences");
	 var unSubscribeMsg = ""
	 if ((emailId != "null" || emailId != "undefined" || emailId != "") && (Stringify(pubId) != "null" && pubId != ''))
	 {
	  listIdArray = pubId.split(',');
	  for(i=0 ; i<listIdArray.length;i++)
	  {
	   listId = listIdArray[i];
	   lists = List.Retrieve({Property:"ID",SimpleOperator:"equals",Value:listId});
	   listKey = lists[0].CustomerKey;
	   listName = lists[0].ListName;
	   myList = List.Init(listKey);
	   //status = myList.Subscribers.Unsubscribe(emailId);
	   var subscriberStatus = Subscriber.Retrieve({Property:"SubscriberKey",SimpleOperator:"equals",Value:emailId});
	   var SubscriberKey = subscriberStatus[0].SubscriberKey;
	   var EmailAddress = subscriberStatus[0].EmailAddress;
	   var Status = subscriberStatus[0].Status;
	   var newSubscriber = 
			      {
				"EmailAddress": EmailAddress,
				"SubscriberKey": SubscriberKey,
				"Status": "Unsubscribe"
			      };
	   var status = myList.Subscribers.Unsubscribe(newSubscriber);
	   if (status == "OK") 
	   {
	     if(unSubList == "")
	       unSubList = listName
	     else
	       unSubList = unSubList + "<br>" + listName;
	   }
	   else
	   {
	    if(unSubscribeMsg == "")
	      unSubscribeMsg = "You have not subscribed to publication list..";
	   }
	  }
	  if(unSubscribeMsg == "")
	   unSubscribeMsg = "You have now succesfully unsubscribed from publication list.";
	 }
	else
	  Write("Email Address is NULL");
	if(unSubscribeMsg != "")
	  Write(unSubscribeMsg + "<br>");
	/*UNSUBSCRIBE ALL CODE END*/
	</script>
 </body>
</html>

Advertisements
Share this Post

About crmTech

Sharad is working from more than 11 years in IT industry. He has giving his technical services in CRM for telecom, automobile, retail, finance industry in Sales, Service and Marketing. He has primary skill in Siebel and Salesforce Marketing Cloud. Apart from this he has worked in Google Cloud, Wordpress blogging, SEO optimization, Short film making, mobile apps creation,logo design creation for his own interest.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.