This article explains how to create a link in an email message with an expiration date using AMPscript in Salesforce Marketing Cloud.
Not a fan of reading? Jump to the code snippet.
How it works
In order to create a link with an expiration date, we need to build an email with an encoded link and a page that will redirect the customer to another page upon success or fail.
Email message
There is no need to use Base64Encode or any other encoding method if we use the CloudPagesURL function.
Please remember that adding RedirectTo function allows us to avoid any errors when additional parameters are added automatically to every link of the email (tracking or likewise).
The date parameter printed by the Now function will represent the date of when the email was sent and provides us with a reference point.
%%[
SET @RedirectPageID = 1234
SET @SendDate = SystemDateToLocalDate(NOW())
SET @ExpiredIn = 1
SET @Destination = 'http://example.com'
SET @Fallback = 'http://google.com'
]%%
<a href="%%=RedirectTo(CloudPagesURL(@RedirectPageID,'SendDate',@SendDate,'ExpiredIn',@ExpiredIn,'Destination',@Destination,'Fallback',@Fallback)=%%">
Click me
</a>
Redirect page
The only goal of this page is to check if the link has expired and redirect the customer to another page, where the data will be processed accordingly.
Once we capture the send data and the number of days after which the link is expires, it’s very easy to create a comparison using the DateDiff function.
%%[
SET @SendDate = QueryParameter("SendDate")
SET @ExpiredIn = QueryParameter("ExpiredIn")
SET @Destination = QueryParameter("Destination")
SET @Fallback = QueryParameter("Fallback")
SET @Today = SystemDateToLocalDate(NOW())
IF DateDiff(@SendDate,@Today,'D') >= @ExpirationInDays THEN
Redirect(@Destination)
ELSE
Redirect(@Fallback)
ENDIF
]%%
Note that the DateDiff function will result in the error 500 if one of the compared values is null or empty. It’s always better to add an extra condition in order to avoid our code being broken.
%%[
SET @SendDate = QueryParameter("SendDate")
SET @ExpiredIn = QueryParameter("ExpiredIn")
SET @Destination = QueryParameter("Destination")
SET @Fallback = QueryParameter("Fallback")
SET @Today = SystemDateToLocalDate(NOW())
IF NOT EMPTY(@SendDate) AND NOT EMPTY(@ExpiredIn) THEN
IF (DateDiff(@SendDate,@Today,'D') >= @ExpiredIn) THEN
Redirect(@Destination)
ELSE
Redirect(@Fallback)
ENDIF
ELSE
Redirect(HTTPRequestHeader("Referer"))
ENDIF
]%%
That’s it folks! This example supports the date difference in days, but you are obviously free to use whatever time measure you like.
Have I missed anything?
Please poke me with a sharp comment below or use the contact form.
Its Throwing an error (Im New to Ampscript and SFMC )
Add Content Preview and TestBACK
×The subscriber preview failed to generate. Review the details, correct all issues, and try again.
An error occurred when attempting to resolve a function call. See inner exception for detail. Function Call: RedirectTo(CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback) Index: 15555 TransCode Type: HTML Content Type: HTML Substitution Level: Subscriber Message Context: Preview
At least one open parenthesis [(] does not have a matching close parenthesis [)] Function Call: RedirectTo(CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback) Parameter Value: CloudPagesURL(@RedirectPageID,’SendDate’,@SendDate,’ExpiredIn’,@ExpiredIn,’Destination’,@Destination,’Fallback’,@Fallback Missing Close Parenthesis Count: 1
you forgot a closing )
I am facing these errors.
The subscriber preview failed to generate. Review the details, correct all issues, and try again.
An error occurred when attempting to evaluate a RedirectTo function call. See inner exception for details.
An error occurred when attempting to evaluate a InvokeCloudPagesURL function call. See inner exception for details.
Invalid value specified for function parameter. Function Name: CloudPagesURL Parameter Name: PageID Parameter Ordinal: 1 Parameter Type: Numeric Submitted Value: ClientID: 514014883 JobID: 0 ListID: 0 BatchID: 0 SubcriberID: 0 Data Source Type: CustomObject Data Source ID: 92da9d48-a89b-ec11-ba35-48df37e63c5d
Hi Amulya, it seems to me that CloudPagesURL has an issue. Did you feed with a valid PageID from the correct Business Unit?