IF statement in AMPScript

dan123 picture dan123 · Jul 18, 2014 · Viewed 29.9k times · Source

I have a little problem with if statement as it populates only last elseif

%%[IF @Add3 == "Y" AND @Add4 == "Y" AND @Add5 == "N" THEN
        SET @WhichNom1 = "1"
        SET @WhichNom2 = "2"

ELSEIF @Add3 == "Y" AND @Add4 == "N" AND @Add5 == "Y" THEN
        SET @WhichNom1 = "1"
        SET @WhichNom2 = "3"

ELSEIF @Add3 == "N" AND @Add4 == "Y" AND @Add5 == "Y" THEN
        SET @WhichNom1 = "1"
        SET @WhichNom2 = "3"
]%%
@WhichNom1
@WhichNom2
%%[ENDIF]%%

The code above will display two variables when the last ELSEIF is TRUE. What do I need to do to check 3 statements and display WhichNom1 and WhichNom2 for every scenario?

Thanks Dan

Answer

Robert' DROP TABLE students picture Robert' DROP TABLE students · Jul 18, 2014

You're only printing the variables if the last elseif is true.

Move them like this:

...    
ELSEIF @Add3 == "N" AND @Add4 == "Y" AND @Add5 == "Y" THEN

        SET @WhichNom1 = "1"
        SET @WhichNom2 = "3"
]%%
%%[ENDIF]%%

@WhichNom1
@WhichNom2

So they're outside the IF/ELSEIF. That way they'll always be printed, but the variables will be set differently based on the clauses.

EDIT:

If you only want the variables printed if one of the statements is true, then you would (as you have mentioned) need to print them within the if statement, OR you could:

SET @WhichNom1 = ""
SET @WhichNom2 = ""

before the if statements, then after them do:

IF @WhichNom1 != "" THEN @WhichNom1
IF @WhichNom2 != "" THEN @WhichNom2

so you only print them if they've been set to something other than "".