[SalesForce] display value from data extension in email for a record with AMPscript

Right now, I have this AMPscript that displays first name of the subscriber from the data extension since I am sending email to data extension as audience.

%%[
var @fname
set @fname = [First-Name]
]%%

Dear %%[IF not empty(@fname) THEN]%%%%=v(@fname)=%%%%[ELSE]%%Customer%%[ENDIF]%%

But, when I try to add more columns from the same data extension so that I can display value from address, title etc I get error in my AMPscript?

Can anyone please clarify if my ampscript is correct? if no, how can i correct it?

This is what I got so far:

%%[
var @fname, @decolumn_2, @decolumn_3
set @fname = [First-Name]
set @decolumn_2 = [DE-Column-2]
set @decolumn_3 = [DE-Column-3]
]%%

Dear %%[IF not empty(@fname) THEN]%%%%=v(@fname)=%%%%[ELSE]%%Customer%%[ENDIF]%%


Displaying other information below:

%%=v(@decolumn_2)=%% | %%=v(@decolumn_3)=%%

Best Answer

The issue could stem from if any of those columns are empty or null values. AMPscript can sometimes have issues with variables being set to null and throw errors.

To better handle null or empty values, I would recommend using the AttributeValue() function for setting your variables.

Example:

%%[

  VAR @fname, @decolumn_2, @decolumn_3

  SET @fname = AttributeValue("First-Name")
  SET @decolumn_2 = AttributeValue("DE-Column-2")
  SET @decolumn_3 = AttributeValue("DE-Column-3")

]%%

  Dear %%[IF not empty(@fname) THEN]%%%%=v(@fname)=%%%%[ELSE]%%Customer%%[ENDIF]%%


  Displaying other information below:

  %%=v(@decolumn_2)=%% | %%=v(@decolumn_3)=%%