0

I'm learning AMP Script and I need to compare a variable with different countries and I'm wondering if, as dynamic block in Marketing Cloud, is it possible to compare a variable with different values. For example:

%%[IF @value == 'Spain, France, Canada' THEN]%%
%%=ContentBlockByName(@test)=%%
%%[endif]%%

In addition, is it possible to do something similar to:

%%[IF @value1 == Spain OR @value2 == 'Latin America' THEN]%%
%%=ContentBlockByName(@test)=%%
%%[endif]%%

Thank you so much, Anna

3

There are two ways to accomplish this, if there are only a couple comparisons then you can use the OR statement, but if it is a large list, it may make more sense to create a sort of delimited string and utilize the IndexOf() function. (or you can build the delimited string and utilize as a rowset as well).

Option 1:

%%[ SET @value = AttributeValue("myCountry")

    IF @value == "Spain" OR @value == "Germany" THEN ]%

     YOUR CONTENT

%%[ endif ]%%

Option 2a:

%%[

    SET @value = AttributeValue("myCountry")
    SET @lookupValue = CONCAT("|",@value,"|")
    SET @countries = "|Spain|France|Canada|"

    IF IndexOf(@countries,@lookupValue) > 0 THEN ]%%

     YOUR CONTENT

%%[ endif ]%%

if you notice, my delimited string starts and ends with pipes | on every entry. This is to prevent partial matches, e.g. day would match not just "day", but also match "today" - giving a false positive. Which is why I created a @lookupValue var, which adds in the | to the value.

Option 2b:

%%[

    SET @value = AttributeValue("myCountry")
    SET @countries = "Spain|France|Canada"

    SET @rowSet = BuildRowsetFromString(@countries,"|")

    FOR @i=1 TO Rowcount(@rowSet) DO

      set @row = Row(@rowset,@i)
      set @country = Field(@row,1)

      IF @value == @country THEN

        SET @pass = 1

      ENDIF

    NEXT @i 

 IF @pass == 1 THEN ]%%

     YOUR CONTENT

%%[ endif ]%%

This one will take the provided delimited string, turn it into a rowset (via BuildRowsetFromString()) and then loop through it comparing the values and if they match, it sets the @pass var to 1. There is then a conditional IF statement afterwards that will display the content if @pass == 1.

  • 1
    Thank you @Gortonington for your answer and your examples! Finally I have chosen the option 2 and it helps me a lot. Have a good day.Anna – Anna Journeys May 24 at 14:24
0

Reference the help page 'AMPscript Language Elements'

Use join operators to combine multiple conditions.

AND - Both conditions must be true OR - Either condition must be true The NOT operator reverses the result of any Boolean expression.

NOT - Reverses the Boolean expression Control the expression evaluation using parentheses:

(BooleanExpression AND BooleanExpression) OR (BooleanExpression AND BooleanExpression)

  • Thank you for your answer @EazyE! Anna – Anna Journeys May 24 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.