[SalesForce] Using ampscript variables to contain if conditions

I'm looking for a way to simplify my code. Currently we have a list of stores (pricezone) that can't receive a certain type of product in an email for legal reasons. As a result, when we're composing emails we often times end up using this code many times throughout the email:

if pricezone != "2" and pricezone != "3" and pricezone != "4" and pricezone != "9" and pricezone != "10" and pricezone != "11" and preferredstore != "803" and preferredstore != "804" then
/* Show restricted content here */
else
/* show nothing */
endif

I'm wondering if I can set a global variable for this combination of stores and zones, so that I could simplify the code for all our templates to something like this:

set @contentblackout = 'pricezone != "2" and pricezone != "3" and pricezone != "4" and pricezone != "9" and pricezone != "10" and pricezone != "11" and preferredstore != "803" and preferredstore != "804"'

if treatascontent(@contentblackout) then
/* show restricted content */
else
/* show nothing */
endif

I tried setting up a test for this, and it doesn't error out the email, but it also still doesn't filter the content properly. This says to me that the treatascontent() function probably isn't the right way to go. Anyone have any ideas if this can be done some other way, or if I'm making a syntax error?

Best Answer

Sorry about the first answer. I assumed standard scripting would work with AMPScript. My bad.

What you can do instead is just move the entire IF statement to the top.

%%[
var @showRestrictedContent
if pricezone != "2" and pricezone != "3" and pricezone != "4" and pricezone != "9" and pricezone != "10" and pricezone != "11" and preferredstore != "803" and preferredstore != "804" then
    set @showRestrictedContent = "TRUE"
else
    set @showRestrictedContent = "FALSE"
endif
]%%

Then you can use the:

%%[
if @showRestrictedContent == "TRUE" then
/* show restricted content */
else
/* show nothing */
endif
]%%

There are several ways of doing what you are doing. Another is to just use dynamic content blocks to display the content. That way you won't have to use code at all (depending on the skill and preferences of the editors).

Hope this works for you.

Related Topic