# Defensive AMPscript coding and language choice

*Marketing Cloud Engagement Developer Study Guide · Programmatic Languages · Last updated November 3, 2026 · https://www.certifyforce.com/study-guide/marketing-cloud-engagement-developer/programmatic-languages/defensive-ampscript-coding-and-language-choice*

When to choose AMPscript or SSJS and why not to mix them, plus the habits that keep scripts from failing: AttributeValue(), fewer lookups, upserts instead of inserts, sanitized TreatAsContent() input, content-block fallbacks, URL encoding, testing, NotSent and View as a Web Page.

- Certification: Salesforce Marketing Cloud Engagement Developer
- Exam section: Programmatic Languages
- Study guide: https://www.certifyforce.com/study-guide/marketing-cloud-engagement-developer

**Official exam objective:**

> Implement standard development best practices using Marketing Cloud programming languages.

## What the exam expects

- Choose AMPscript or SSJS for a requirement, and keep one language per new landing page.
- Separate browser-side JavaScript from server-side code on a CloudPage.
- Write scripts that survive missing attributes, missing content blocks, duplicate keys and unsafe input.
- Reduce data extension calls and encode values placed in links.
- Test scripts, find why messages didn't send, and keep View as a Web Page faithful to the sent message.
- Key terms: **Platform functions** (SSJS functions for messages, landing pages and applications); **Core library** (SSJS functions for landing pages and applications); **AttributeValue()** (a safe attribute read that returns null); **NotSent tracking extract** (the list of messages that didn't send, with reasons).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| Email language | AMPscript or Platform SSJS; Core only on landing pages and apps | [Server-Side JavaScript](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_serverSideJavaScript.html) |
| Mixing languages | Possible but not encouraged; one language per new landing page | [Use SSJS with AMPscript](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_useSSJSAMPscript.html) |
| SSJS limits | No DOM, no external libraries | [Server-Side JavaScript](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_serverSideJavaScript.html) |
| Missing attribute | AttributeValue() returns null instead of failing | [AttributeValue()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-utilities/mc-ampscript-reference-utilities-attribute-value.html) |
| Email writes to the send's data extension | Use UpsertDE(); InsertDE() hits a duplicate key | [Order of Operations](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-guide-language-basics-order-of-operations.html) |
| InsertData() failure | 500 error on duplicate key or missing required column | [InsertData (SSJS)](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_platformDataExtensionInsertData.html) |
| ContentBlockByKey() default | Errors on a missing block; status `-1` when not found | [ContentBlockByKey()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-content/mc-ampscript-reference-content-block-by-key.html) |
| URLEncode() defaults | Only spaces in a parameter string become `%20` | [UrlEncode()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-http/mc-ampscript-reference-http-url-encode.html) |
| Why messages didn't send | NotSent tracking extract, with reasons | [Write Your Best Scripts](https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-programmatic-languages/write-your-best-scripts-with-programmatic-languages-) |
| View as a Web Page | Reference the send's BatchID | [Write Your Best Scripts](https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-programmatic-languages/write-your-best-scripts-with-programmatic-languages-) |

## Check yourself

- Why should AttachFile() be added to a message with care even when the feature is enabled?
- When does UpsertDE() leave the data extension unchanged and return 0?
- What does a ContentBlockByKey() status code of 0 mean?
- When does URLEncode() convert non-ASCII characters in a parameter string?

## Sources

- [Marketing Cloud Engagement Developer Exam Guide](https://help.salesforce.com/s/articleView?id=005298954&language=en_US&type=1)
- [Write Your Best Scripts with Programmatic Languages (Trailhead)](https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-programmatic-languages/write-your-best-scripts-with-programmatic-languages-)
- [Server-Side JavaScript](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_serverSideJavaScript.html)
- [Use Server-Side JavaScript with AMPscript](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_useSSJSAMPscript.html)
- [SSJS Syntax Guide](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_syntaxGuide.html)
- [AttributeValue() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-utilities/mc-ampscript-reference-utilities-attribute-value.html)
- [Order of Operations (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-guide-language-basics-order-of-operations.html)
- [UpsertDE() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-upsert-de.html)
- [InsertDE() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-insert-de.html)
- [InsertData (SSJS)](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_platformDataExtensionInsertData.html)
- [UpsertData (SSJS)](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_platformDataExtensionUpsertData.html)
- [TreatAsContent() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-content/mc-ampscript-reference-content-treat-as-content.html)
- [ContentBlockByKey() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-content/mc-ampscript-reference-content-block-by-key.html)
- [UrlEncode() (AMPscript)](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-http/mc-ampscript-reference-http-url-encode.html)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 14 linked practice questions, and unlimited timed mock exams are included with the Marketing Cloud Engagement Developer pack (USD $19): https://www.certifyforce.com/practice-exams
