---
schema_version: "1"
id: 3c135e41e04c091732fdb8f8f144739f34e4bfaaef0208cdb3c80c778c5a147f
title: Order of Operations
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/guide/language-basics/mc-ampscript-guide-language-basics-order-of-operations.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-guide-language-basics-order-of-operations.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 28f48523fe1876b36e93c3b1ebb56fbf6c3836efc7b627a4edab8d007ed1a22a
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# 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.

## AMPscript Processing in Email Sends

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()](/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-insert-de.html) 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()](/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-upsert-de.html) in the email to prevent primary key conflicts.

## Processing of Content Sections in an Email

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.
