crmTech/ July 9, 2018/ Salesforce Marketing Cloud/ 0 comments

Find below the sample code SSJS for custom subscription page.

UI FORM SECTION : Web page to receive the input from User.

<!DOCTYPE html>
 <html>
 <title>Subscription Page</title>
 <body>
 <h2>Subscription Page</h2>
 <form action="URL OF PUBLISHED PROCESSING PAGE CLOUD PAGE" method="post">
 Enter Your Email Address Here:
 <input type="text" name="emailId" value="">
 <br>
 <p>Subscribe to Publication List</p>
 <input type="radio" name="listId" value="50646"> A MY LIST <br>
 <input type="radio" name="listId" value="53308"> B PUBLICATION LIST<br>
 <input type="radio" name="listId" value="53372"> C PUBLICATION LIST<br>
 <br><br>
 <input type="submit" value="Subscribe Me">
 </form>
 </body>
 </html>

PROCESSING PAGE SECTION: This will actual process the data which is received from UI FORM Section

<html>
	<body>
		<script runat=server>
			
			Platform.Load("Core","1");
			var emailId = Platform.Request.GetFormField('EmailId');
			var listId = Platform.Request.GetFormField('listId');
			var data = DataExtension.Init("SSJS_Preferences");
			var SubKey;
			
			Write("Your Email id : " + Stringify(emailId) + "<br>");
			Write("Your List Id : " + Stringify(listId) + "<br>");
			
			if ((Stringify(emailId) != "null" && emailId != '') || (Stringify(listId) != "null" && listId != ''))
			{
				//Get the ListKey/CustomerKey for which the user has selected the List
				var lists = List.Retrieve({Property:"ID",SimpleOperator:"equals",Value:listId});
				var listKey = lists[0].CustomerKey;
				var listStatus = lists[0].Status;
				var listName = lists[0].ListName;
				//Add the Subscriber to list
				var myList = List.Init(listKey);
				var status = myList.Subscribers.Add(emailId);
				if (status == "OK")
				{
					Write("Thank You for Subscribing the list : " + Stringify(listName) + "<br>");
				}
				else
				{
					//Start : Getting Subscriber Key and Subscriptiton Staus of Subscriber
					var subObj = Subscriber.Retrieve({Property:"EmailAddress",SimpleOperator:"equals",Value:emailId});
					for(i = 0 ; i<subObj.length; i++)
					{
						SubKey = subObj[i].SubscriberKey;
					}
					//End : Getting Subscriber Key
					var subObj = Subscriber.Init(SubKey);
					var listArray = subObj.Lists.Retrieve();
					var listStatus = listArray[0].Status;
					if(listStatus == "Active")
					{
						Write("You are already in Subscribtion list : "+ Stringify(listName) + "<br>");
					}
					else
					{
						var status = myList.Subscribers.Upsert(emailId,{"Status":"Active"});//If Subscriber already existwith not Active status then we make him Active
						if (status == "OK")
						{
							Write("You are already in Subscribtion list but Not Activated. We have Activated the list: "+ Stringify(listName) + "<br>");
						}
					}
				}
			}
			else
			{
			Write("Please enter Email Id and Select the Publication list which you want to subscribe.");
			}
		</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.