🎉 Come hang out — the Discord server is live and free to join!

background shape
background shape

Issue with AMPScript domain function in SMFC

Sometimes the unexpected currents of salesforce marketing cloud will take you to the issues you never experienced before. Recently we discovered that the function domain is not working within condition used in the email studio.

Wanted to add a restriction on our transactional email service to send emails only when the domain is whitelisted. Simple, right? We thought so too.

I knew there was a function in AMPScript that directly extracts the email domain from a given string.

Domain() function problem when sending email from salesforce marketing cloud

We used domain() function that does what it says. Extracts domain from any valid email string.

%%[ IF NOT EMPTY(Lookup("domain_whitelist","domain","domain", Domain(AttributeValue("EmailAddress")))) THEN]%%
  //send email
%%[ELSE]%%

Everything worked in the preview of the email in the content builder (email studio) — no questions asked. “QA” passed with five stars, all good to go. Ready for testing by the other team that like to send API calls.

Now a bit later we’re hearing back from the team that the template stopped working.
We assumed the issue was caused by the newly added AMPScript section and our domain feature.

So, we rebuilt the domain extraction logic using basic AMPScript functions. After clearing the cache, emails started flowing in again.

%%[
SET @email = LowerCase(Trim(AttributeValue("EmailAddress")))
SET @atIndex = IndexOf(@email, "@")
SET @domain = Substring(@email, Add(@atIndex, 1), Length(@email))
  
IF NOT EMPTY(Lookup("domain_whitelist","domain","domain", @domain)) THEN]%%
  //send email
%%[ELSE]%%

â›” Do not use Domain() nested within another function!

I was not able to sleep and tried the domain() function work and found out that it has issues only when used inline. When you save results of the function to new variable and use the variable in the condition all works as expected

%%[ 
  SET @email = LowerCase(Trim(AttributeValue("EmailAddress")))
  SET @domain = Domain(@email)
  IF NOT EMPTY(Lookup("domain_whitelist", "domain", "domain", @domain)) THEN 
]%%
// send email
%%[ ELSE ]%%

đź’ˇ Possible explanation for why Domain() fails when nested within another function

We encounter similar issue with nested indexOf function within one line AMPScript code call. It seems like AMPScript sometimes fails to evaluate nested function calls reliably when used inline inside another function like Lookup(). Especially when functions like Domain() depend on dynamic content (AttributeValue()), the engine might not process them in the expected order — resulting in empty or null values.

By assigning Domain(@email) to @domain, we’re forcing the evaluation before the Lookup() is run — which stabilizes the logic.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus gravida dignissim volutpat. Vestibulum ante ipsum primis in faucibus orci lu

đź”’ This content is for Premium Subsribers only.

Please log in to preview content. Log in or Register

You must log in and have a Premium Subscriber account to preview the content. When upgrading, please provide the same email address as for your WordPress account; otherwise, we will not be able to link your Premium membership. Please also provide your Discord username or contact me directly to get access to the Discord community once your subscription is purchased. You can subscribe even before your account is created; the subscription will be linked to your WordPress email address.

Premium Subscriber

9.99 € / Month

  • Access to exclusive blog content
  • In-depth articles not available online
  • Insights from industry experts
  • Free Discord community invite
  • Unlimited questions in Solution Station
  • Limited seats at this price

Oh hey there đź‘‹
I’ve got something special for you—free scripts every month!

Sign up now to receive monthly premium Salesforce Marketing Cloud script examples straight to your inbox.

We don’t spam! Read our privacy policy for more info.

Share With Others

Leave a Comment

Your email address will not be published. Required fields are marked *

MarTech consultant

Marcel Szimonisz

Marcel Szimonisz

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms—particularly Salesforce Marketing Cloud and Adobe Campaign.

Related posts