0

I'm using this AMPscript to pull in a father's first name:

%%[ SET @rows = LookupRows("Contact_Salesforce","Id", _subscriberkey) 
    SET @row = row(@rows,1) 
    SET @firstName = field(@row,"firstName") 
    SET @Father_First_Name__c = field(@row,"Father_First_Name__c") 
    IF NOT EMPTY(@Father_First_Name__c) THEN ]%% 
Dear %%=v(@Father_First_Name__c)=%%
%%[ENDIF]%%

[(firstName) is used elsewhere in the email]

I need this to show simply "Hello,", if there is no value in the @Father_First_Name__c field.

How do I do this please?

5

I'm a fan of using the inline iif() AMPscript function.

%%[ 
SET @rows = LookupRows("Contact_Salesforce","Id", _subscriberkey) 
if rowCount(@rows) > 0 then
  SET @row = row(@rows,1) 
  SET @firstName = field(@row,"firstName") 
  SET @Father_First_Name__c = field(@row,"Father_First_Name__c")
endif
]%% 

%%=iif(empty(@Father_First_Name__c),'Hello',concat("Dear ",@Father_First_Name__c))=%%,

The iif above is checking if @Father_First_Name__c is empty, and if it is, "Hello" is returned and output in your email. If it is not empty, the concat of "Dear " (with a space) and our @Father_First_Name__c variable is output. Note that the comma is after the iif() because it is applicable in both scenarios.

Also, I'd recommend adding the rowcount check as seen above to make sure your LookupRows() returned a row, in order to avoid an Invalid index error.

1
  • 1
    +1 IIF is the better answer, less code. IF statement in email/web page become really hard to manage. – Frogmouth Jan 5 '20 at 19:28
5

You were literally 90% of the way there. You just need to put in an ELSE statement with the required content:

%%[ SET @rows = LookupRows("Contact_Salesforce","Id", _subscriberkey) 
    SET @row = row(@rows,1) 
    SET @firstName = field(@row,"firstName") 
    SET @Father_First_Name__c = field(@row,"Father_First_Name__c") 

    IF NOT EMPTY(@Father_First_Name__c) THEN ]%% 
Dear %%=v(@Father_First_Name__c)=%%
%%[ELSE]%%
Hello,
%%[ENDIF]%%

and then it will output: Dear John if name present or Hello, if not.

4

Reference: AMPscript 201

enter image description here

Seems like you're nearly there, just need to add an "else" condition to handle just showing Dear if the name is blank.

IF NOT EMPTY(@Father_First_Name__c) THEN ]%% 

Dear %%=v(@Father_First_Name__c)=%%

%%[ELSE]%%

Dear

%%[ENDIF]%%

Your Answer

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.