0

Hi All,

I would like to request assistance if this is possible using Ampscript?

Data Extension
Q1 = YES
Q2 = YES
Q3 = NULL
Q4 = YES

Email Content:
Dear Customer,
Here is the summary of your answer to your question.
%%=v(@Q1)=%%%%=v(@Q2)=%%%%=v(@Q3)=%%%%=v(@Q4)=%%

Preview
Dear Customer,
Here is the summary of your answer to your question.
YESYESNULLYES


What I want is, if the variable detects a YES on it, a line break(br) will appear and if NULL(Q3) Q4 will go upward.

Expected Outcome
Dear Customer,
Here is the summary of your answer to your question.
YES
YES
YES

3
  • Why don’t you just add the <br> tags in between each variable output and add IF statements for each one to check for the NULL value before outputting it so they don’t get displayed on the output? Commented May 20, 2021 at 9:09
  • Is there an ampscript code in which I can attach a line break. Example - %%=v(@Q1)<br>=%%. I'm not sure if this is correct. Commented May 20, 2021 at 9:21
  • You would need to use Concat function for that, check out my answer where I make use of it to combine variable and break. Commented May 20, 2021 at 10:17

2 Answers 2

1

Even though there is nothing wrong with the answer provided by Johannes, I will recommend checking out the often overlooked IIF function. It will allow you to reduce the code quite significantly.

%%=IIF(EMPTY(@Q1),"",CONCAT(@Q1,"<br/>"))=%%
%%=IIF(EMPTY(@Q2),"",CONCAT(@Q2,"<br/>"))=%%
%%=IIF(EMPTY(@Q3),"",CONCAT(@Q3,"<br/>"))=%%
%%=IIF(EMPTY(@Q4),"",CONCAT(@Q4,"<br/>"))=%%

Also, you might want to use AttributeValue when fetching values from your data extension prior to using the values in your code, to make your code more robust:

SET @Q1 = AttributeValue("Q1")
SET @Q2 = AttributeValue("Q2")
SET @Q3 = AttributeValue("Q3")
SET @Q4 = AttributeValue("Q4")
3
  • Wow, works perfectly thanks :) Commented May 20, 2021 at 10:22
  • If the DataExtension uses "NULL" as a real text you would need to have more conditions than "EMPTY @q1" but also check for "null" as text. otherwise you will output "null" ;) Commented May 20, 2021 at 10:47
  • You're welcome, @HarveyLinarez. Remember, instead of posting a thank you comment (which I indeed do appreciate) to mark the question as accepted. You can read here, why it is a good idea. Commented May 21, 2021 at 8:43
0

I guess you would like to have something like this:

%%[
IF @Q1 == "YES" THEN
]%%
%%=v(@Q1)=%%<br>
%%[
ENDIF
IF @Q2 == "YES" THEN
]%%
%%=v(@Q2)=%%<br>
%%[
ENDIF
IF @Q3 == "YES" THEN
]%%
%%=v(@Q3)=%%<br>
%%[
ENDIF
IF @Q4 == "YES" THEN
]%%
%%=v(@Q4)=%%<br>
%%[
ENDIF
]%%

It checks if the variable is "YES", when it is yes it outputs it otherwise it ignores it. It is not the best or shortest or most elegant way to do this but should be eas to understand.

3
  • Perfect. Once last question: what if the word is not YES but a different word/sentence ? Example: IF Q1 is not empty - show the word Commented May 20, 2021 at 10:06
  • For empty values you would combine the statements: IF @Q1 == "YES" AND NOT EMPTY(@Q1) THENfor whole sentenced (they must exactly match) you would have to change the string inside the " " signs. For example IF @Q1 == "The quick brown fox jumps over the lazy dog" AND NOT EMPTY(@Q1) THEN ... Commented May 20, 2021 at 10:58
  • To cover it with Lukas more estatic version of a solution this would be something like %%=IIF(@Q1 == "The quick brown fox jumps over the lazy dog" AND NOT EMPTY(@Q1),"CONCAT(@Q1,"<br/>","))=%% be aware that i completely negated Lukas conditions to be positive and not negative. Therefore i also need to put the output parameter into the second iif parameter and not the third. Commented May 20, 2021 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .