Loading
Feature Disruption - Service Cloud VoiceRead More
Feature degradation | Gmail Email delivery failureRead More

How to Prevent AMPScript from Evaluating in Send Previews or in other contexts

Publish Date: Mar 12, 2024
Description
In some circumstances when building emails, you won't necessarily want AMPScript to evaluate when previewing the email but rather, only evaluate when the message is actually sent to customers. A prime example of this are the AMPScript functions for creating / updating records in Data Extensions or Salesforce. Additionally, the same may be true for when customers view emails as webpages. 

If you have AMPScript in your email to create rows in Data Extensions or records in Salesforce, there is a possibility that upon sending the email to customers, every time they view the email as a webpage, the AMPScript will re-evaluate if we don't include a reference to the message context in our code. 

Additionally, you may notice that AMPScript is inserting multiple rows or creating multiple objects when previewing without a reference to context. This article is aimed to explain the above and demonstrate how to implement this to behave how you want when you are sending and testing.
Resolution

Q: Why is the AMPScript evaluated more than once when I don't include a reference to the message context?
A: The AMPScript is evaluated both in the HTML and Text versions of the email. You can explicitly remove the AMPScript from the Text version of the email and at that point it will only evaluate one time. 


Q: How do I prevent AMPScript from evaluating in Send Previews or when viewing the email as a webpage?
A: You can reference the message context to control exactly what happens and when. The following message contexts are available referencing the following attribute _messagecontext:

SEND - Display the rendered final message for sending to subscriber
PREVIEW - Display the send preview options available within editor
VAWP - Display content
VIEWSENT - Display the non-subscriber link to preview content
FTAF - Display the rendered Forward To a Friend message
LANDINGPAGE - Display a landing page or microsite
VALIDATION - Display information corresponding to the validate option in Marketing Cloud
LINKRESOLUTION - Display resolved dynamic script at click time
SMS - Display SMS message content
SOCIAL - Display Social Forward content
SITE - Display CloudPage content

This is outlined in our help documentation here.

In order to prevent the AMPScript from evaluating during preview or view as a webpage and only evaluate during sending to an inbox, you would wrap your AMPScript in an IF statement to essentially ensure the _messagecontext is SEND rather than PREVIEW or VAWP. 

For more information on creating these IF statements, see the documentation linked here.

Example Code:
 

<html>
<body>

%%[ 
  IF _messagecontext == 'VAWP' THEN
]%%

<p>
 Print my View as a Web Page Content!!
</p>
%%[ ELSE ]%%

<p>
 Print my Standard Content for All Other Contexts!!
</p>

%%[ ENDIF ]%%

<table cellpadding="2" cellspacing="0" width="600" ID="Table5" Border=0><tr><td><font face="verdana" size="1" color="#444444">This email was sent by: <b>%%Member_Busname%%</b><br>%%Member_Addr%% %%Member_City%%, %%Member_State%%, %%Member_PostalCode%%, %%Member_Country%%<br><br></font></td></tr></table><custom name="opencounter" type="tracking">

</body>
</html>


 

Knowledge Article Number

000381182

 
Loading
Salesforce Help | Article