0

I've question about AMPscript. I have if statement as below it is working but if one of the value will be empty then we will have a gap in email. Do you know if there is solution for that?

%%[IF NOT EMPTY (@x) THEN]%% 1nane: %%=v(@x)=%% %%[ENDIF]%%
%%[IF NOT EMPTY (@y) THEN]%% 2name: %%=v(@y)=%% %%[ENDIF]%%
%%[IF NOT EMPTY (@z) THEN]%% 3name: %%=v(@z)=%% %%[ENDIF]%%
%%[IF NOT EMPTY (@w) THEN]%% 4name: %%=v(@w)=%% %%[ENDIF]%%
2
  • 1
    What do you want it to do in case it’s empty?
    – zuzannamj
    Commented Jan 4, 2023 at 12:30
  • @zuzannamj thanks! I've already fixed that. I had to edit html code and put <br> in another place :)
    – Natalia
    Commented Jan 4, 2023 at 14:25

2 Answers 2

1

Going off the question, I am assuming you want to control the numbering you have on name being displayed. So below is best I can think of to handle that without interruption to your current process:

%%[SET @nameCount = 1]%%
%%[IF NOT EMPTY (@x) THEN]%% 
  %%=v(@nameCount)=%%name: %%=v(@x)=%%<br> 
%%[SET @nameCount = ADD(@nameCount,1) ENDIF]%%
%%[IF NOT EMPTY (@y) THEN]%% 
  %%=v(@nameCount)=%%name: %%=v(@y)=%%<br> 
%%[SET @nameCount = ADD(@nameCount,1) ENDIF]%%
%%[IF NOT EMPTY (@z) THEN]%% 
  %%=v(@nameCount)=%%name: %%=v(@z)=%%<br> 
%%[SET @nameCount = ADD(@nameCount,1) ENDIF]%%
%%[IF NOT EMPTY (@w) THEN]%% 
  %%=v(@nameCount)=%%name: %%=v(@w)=%%<br> 
%%[SET @nameCount = ADD(@nameCount,1) ENDIF]%%

But I would instead recommend doing the following to make it simpler and easier for maintenance:

%%[
  SET @nameStr = CONCAT(@x,"|",@y,"|",@z,"|",@w)
  SET @nameRS = BuildRowsetFromString(@nameStr,"|")
  SET @count = 1

  FOR @i=1 TO RowCount(@nameRS) DO
    SET @row = Row(@nameRS,@i)
    SET @field = FIELD(@row,1)
    IF NOT EMPTY(@field) THEN]%%
      %%=v(@count)=%%name: %%=v(@field)=%%<br> 
  %%[ SET @count = ADD(@count,1)
    ENDIF
  NEXT @i ]%%

This will utilize a FOR loop for the output as it is the same in general for each, with a couple variable entries. It also makes editing and modifying easier as it only needs to be done by adding a new CONCAT parameter (e.g. ,"|",@a) and it will then correctly output your expected content. And if you need to make a change to the output, you only have a single place, not multiple, meaning you reduce risk of human error by ensuring its uniform output and not potentially miss one or incorrectly edit one, etc. (for example, how in your question you have the first one @x saying nane instead of name.)

2
  • thanks! :) actually in my case it was just about <br> because I put it in a wrong place and when the field was empty there was not necessary spacing between the rows. But thanks a lot for your advice.
    – Natalia
    Commented Jan 4, 2023 at 14:52
  • @natalia - Glad you were able to solve it! Would you mind putting your solution into a self answer and marking it as correct for others to reference if they have the same issue? Commented Jan 4, 2023 at 14:56
0

Luckily it was small thing. I just needed to update html code and
so it was before not after endif. Thanks to that there is no spacing between the rows if for example name 3 is empty.

%%[IF NOT EMPTY (@x) THEN]%% 1nane: %%=v(@x)=%% <br> %%[ENDIF]%%
%%[IF NOT EMPTY (@y) THEN]%% 2name: %%=v(@y)=%% >br> %%[ENDIF]%%
%%[IF NOT EMPTY (@z) THEN]%% 3name: %%=v(@z)=%% <br> %%[ENDIF]%%
%%[IF NOT EMPTY (@w) THEN]%% 4name: %%=v(@w)=%% <br> %%[ENDIF]%%

You must log in to answer this question.

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