Order of Operations
Marketing Cloud Engagement has consistent rules for processing the AMPscript code in your emails and sites. These rules help make sure your content behaves the way you expect it to.
Marketing Cloud Engagement processes AMPscript code in a batch as the final step in an email send, rather than executing each piece of code sequentially. This rule is important because it impacts which AMPscript function you should use in your email.
For example, you might use data extensions to manage subscriber information. During an email send, Marketing Cloud Engagement writes the subscriber information to the data extension. If the emails in that send use the InsertDE() function to add data to the data extension, they produce a duplicate key error because the subscriber data was added to the data extension as a result of the send. In this situation, use UpsertDE() in the email to prevent primary key conflicts.
The AMPscript interpreter generally processes code from the beginning of a document to the end. However, in an email, there are several components of a message that can contain AMPscript code. Marketing Cloud Engagement processes the AMPscript code in message components in this order:
- Preheader text
- HTML body
- Text body
- Subject line
This order of operations is important, because it impacts the states and values of variables. For example, if you set the value of a variable in the subject line, the value of that variable is null
in the HTML and text body because the AMPscript interpreter processes the subject line last. If you set the value of a variable in the HTML body of the email, that variable has the same value in the text body and the subject line.