0

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
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2

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
]%%
2

To be honest both of the answers need to be commented or clearly described.

You can use this codeblock which is (nearly) the code of Josh de Blank IF you want to display the content Block when one value evaluates to true. This can also mean that you display two content blocks(if both are true) or none (if none of the arguments result in true). If this is what you are searching for you gotta take Josh's. This code has no default, which is sometimes risky (can produce no output or multiple). I would always encourage people to have all possible scenarios in their logic.

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

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

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

You can use this codeblock if you only want to display definitely ONE ContentBlock, even if both Products have "true" in them. This is something you have to keep in mind when you watch this code. When Product1 and Product2 have "true" values, the contentblock 54321 will never be displayed. Why? Because the first IF clause evaluates to true and therefore the ELSEIF clause will never be run. Which maybe is not what you want.

%%[
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")
ELSEIF @Product2 == 'True' THEN
    SET @BodyContent1 = ContentBlockbyId("54321")
ELSE
    SET @BodyContent = ContentBlockbyId("11111")
ENDIF
]%%


This would be my approach:

I would encourage you to use something like this, which is only an addition to the one of CodeRage but maybe a really necessary one. The first condition really needs to be thought of ==> what happens if both Product1 and Product2 have the value true? In this example it takes ONE other unique block and inserts it. And i would only use one variable which is BodyContent and not two BodyContent1 and BodyContent. If you want to display both i would have created either the 98765 content block which inherits both or make 2 variables which are better seperated like @BodyContent_top and @BodyContent_bot.

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

IF @Product1 == 'True' AND @Product2 == 'True' THEN
    SET @BodyContent = ContentBlockbyId("98765")
ELSEIF @Product1 == 'True' THEN
    SET @BodyContent = ContentBlockbyId("12345")
ELSEIF @Product2 == 'True' THEN
    SET @BodyContent = ContentBlockbyId("54321")
ELSE
    SET @BodyContent = ContentBlockbyId("11111")
ENDIF
]%%
  • My bad, I meant to use just one variable for @BodyContent - corrected my anwser – CodeRage Aug 14 at 7:13
2

Here's a leaner way to do it:

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")
ELSEIF @Product2 == 'True' THEN
SET @BodyContent = ContentBlockbyId("54321")
ELSE
SET @BodyContent = ContentBlockbyId("11111")
ENDIF
0

Thank you all!

this code solved what I am trying to achieve:

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

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

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

New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

Alex is a new contributor. Be nice, and check out our Code of Conduct.

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.