As EazyE commented, you are likely running into the "order of operations" (https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/orderOfOperations.html) behavior where multipart MIME emails execute AMPScript twice for each user, once in HTML, then in text.
As seen in the link, the email's code is processed in linear order, top to bottom, for each user. Variables are not reset between processing of the different versions, which is key for the approach further below.
You could just manipulate the text version to not mirror the html version (just write something else into it, which decouples both versions), but this is error prone as no-one ever thinks about the text version. Helps isolate the issue though.
To create code that counteracts this behavior:
Wrap each of the executions in a condition (e.g. I use executeNextOccurrence != false
);
set that condition to "false" after the code you want to run only once.
So: At send time, the message will run your code once in HTML, then set the executeNextOccurrence
condition to false
.
It will then start executing the text version, with the executeNextOccurrence
still being false
(it is not reset between versions) --> your code is skipped after having been run once.
In another instance (i.e. you have to test with your specific code) I solved this the following way. I purposefully include the comment for posterity to understand what this is about.
%%[
IF _messagecontext == "SEND" AND jobid != 0 THEN
IF @executeNextOccurrence != "false" THEN
/* your code with outside effect here */
/* if sendout is done using multipart MIME (which is basically always),
HTML is parsed, then text (https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/orderOfOperations.html),
leading to AMPScript being interpreted twice.
The IF / ELSE / executeNextOccurrence workaround helps to prevent that
code "with outside effects" is run twice - once in HTML, once in text.
*/
SET @executeNextOccurrence = "false"
ENDIF
ENDIF
]%%