[SalesForce] AMPscript if statement not working correctly

I am new to AMPscripts and need some help with the below. What I am trying to do is set up conditions that will populate BodyContent (using content block id) in an email with contact data (not journey data, that's why I used the Lookup) if the contact data attribute value returns true. If the value returns false or null I want to show something else. However, the part that checks for "false" doesn't work (last part) but I can't understand where my syntax is wrong. Also, it would be great to know if there is a shorter or better way to write this script?

SET @id = AttributeValue('SubscriberKey')
SET @Product1 = Lookup('Test_Journey','Product1','SubscriberKey',@id))
SET @Product2 = Lookup('Test_Journey','Product2','SubscriberKey',@id))

IF @Product1 == 'True' THEN
  SET @BodyContent = ContentBlockbyId("12345")

  IF @Product2 == 'True' THEN
    SET @BodyContent1 = ContentBlockbyId("54321")

    IF @Product1 == 'False' AND @Product2 == 'False' THEN
      SET @BodyContent = ContentBlockbyId("11111")

    ENDIF
  ENDIF
ENDIF

Best Answer

Code within an IF statement will execute if the statement resolves to true. By nesting your IF statements, they will only be evaluated if the first IF statement resolves true. In your example you are checking to see if your variables are false after you've already established they are true, so your 3rd IF statement will never resolve to true. Don't nest if you don't need to.

%%[
IF @Product1 == 'True' THEN 
    SET @BodyContent = ContentBlockbyId("12345")
ENDIF 

IF @Product2 == 'True' THEN 
    SET @BodyContent1 = ContentBlockbyId("54321")
ENDIF

IF @Product1 == 'False' AND @Product2 == 'False' THEN 
    SET @BodyContent = ContentBlockbyId("11111")
ENDIF
]%%
Related Topic