Monday, September 7, 2020

Solve Common SFMC Problems with AMPScript (Forms) - Part 1

New to Salesforce Marketing Cloud and wondering what you can do with Ampscript? In this series of posts, I'll show you somethings that can make the "your form doesn't work" emails go away, how to make content blocks more friendly and even how to run ampscript in Automation Studio. Let's start with reducing the number of emails you get from sales telling you that the person can't submit their survey response.

Your Form Doesn't Work

Honestly, I don't know how many times I have heard this one in my career... Sales calls you in a panic because people can't submit to the smart capture form that you set up on a Cloud Page? I must have heard it once or twice a week.

AMPScript can help you stop those emails. It's actually really easy, and can be done on template/content block pages just as easily as if you were developing code for a custom landing page. The rowcount AMPScript function is your friend and this actually works best when you're passing a parameter through the click string. For example if your target data extension contains email address or another unique identifier that populates a hidden field, you'll be set.

For this example, we are passing through the unique identifier using the parameter of "c". The click would look something like https://landingpage.com?c=%%subscriberkey%%. Then we'll place place this AMPScript above your header in an html block on the landing page:

%%[
var @rows, @row, @rowCount, @lookup
set @lookup = RequestParameter('c')
set @rows = LookupRows("TargetDataExentions","SubscriberKey", @lookup)
set @rowCount = rowcount(@rows)
]%%

The RequestParameter command tells SFMC to look in the click string to find the record's unique identifier. If you're familiar with this you're probably putting that in your hidden field for the value. If you are, then you'll replace that with %%=v(@lookup)=%%.

Then above your Smart Capture form add an html block. In that block you'll put:

%%[ IF @rowCount <= 0 THEN ]%%

Then below your Smart Capture form, add another html block. In that block you'll put: 

%%[ ELSE ]%%

Then a free form block, that includes a statement like: "We appreciate your enthusiasm, but you've already replied to our survey! If you need assistance or would like to add to your response, please contact us as surveys@yourcompany.com".

Follow that block with a final html content block with the following:

%%[ ENDIF ]%%
 
Now John Smith can open the email and click the survey link and submit his responses. But a week goes by and he sees the email again, and can't remember if he submitted the survey or not, when he clicks again, automatically the landing page will tell him that he's already submitted. No need for him to go through trying to submit all over again and then calling sales to say the submit button is broke.