Search code examples
javascriptsalesforce-marketing-cloudexpression-evaluationampscriptcontrolling

SFMC AMPscript Understanding Controlling Expression Evaluation unclear


The below expression is not making sense to me. I'm having a hard time understand why it will result in "free shipping". Can someone elaborate on the explanation?

%%[

var @statusTier, @amount, @freeShipping
set @statusTier = "Bronze"
set @amount = 300

if @statusTier == "Bronze" or @statusTier == "Silver" and @amount > 500 then
  set @freeShipping = true
endif

]%%

<p>You %%=Iif(@freeShipping == true, "qualify","do not qualify")=%% for free shipping.</p>

Output:
The join operators in the above if statement will be evaluated as a single expression and will produce the following result:

<p>You qualify for free shipping.</p>

From my understanding the set amount of 300 is not > 500 therefore this should not have been a true statement and should output "You do not qualify for free shipping.". I'm a missing something here? Please help, I'm a newbie to AMPscript with little knowledge to JavaScript.

Original THE AMPSCRIPT GUIDE post: https://ampscript.guide/controlling-expression-evaluation/

Thank you for you input in advance!


Solution

  • The example in the ampscript guide post is demonstrating order of operations.

    When there are no parentheses around the OR condition, the code will evaluate as true because it's looking at the entire condition as a single expression.

    When you add parenthesis:

    if (@statusTier == "Bronze" or @statusTier == "Silver") and @amount > 500
    

    The OR condition within () will be evaluated first and then the AND condition will be evaluated. This will result as false because @amount is in fact less than 500.