0

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?

1 Answer 1

0

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.

9
  • This didn't work for me, I got this error: Invalid Content: 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") Are you sure parenthesis are the correct syntax? Commented Jan 26, 2017 at 12:44
  • I also tried using quotes instead, and using double quotes on the outside and single quotes inside, no error with those, but it didn't work either. I feel like the if is looking at the variable and doesn't know what to do since there's no operator, which is why I tried the treatascontent() function, since I just want it to place the text there. Commented Jan 26, 2017 at 13:06
  • My bad. I assumed standard script would work with AMPscript. Commented Jan 26, 2017 at 13:08
  • @Kevin: Edited my original answer. Commented Jan 26, 2017 at 13:20
  • I had to make another couple syntax adjustments, but the concept does work. Here's the final code I ended up with: var @contentblackout 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 @contentblackout = "N" else set @contentblackout = "Y" endif if @contentblackout == "N" then /* Show restricted content */ else /* Show nothing*/ endif Commented Jan 26, 2017 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .