0

I wanted to send a SMS and only display the "FirstName" of the recipient if this variable is under a certain character length.

I was looking at how to do this and can see two options:

Length

%%[IF (Length(@FirstName)>8 THEN]%%

Hi, etc etc

%%[ELSEIF (Length(@FirstName)<8 THEN]%%

Hi %%FirstName%%, etc etc

or I could use Substring

%%=Substring("FirstName",1,8)=%%

I've searched quite extensively on here and couldn't find a question that covers this.

Is this the correct method to use or is there another way that's considered best practise?

1
  • What app are you using for the sending of the SMS?
    – Nonick
    Dec 14, 2016 at 3:58

4 Answers 4

3

I would recommend you to use AttributeValue AmpScript function to check FirstName field value.

%%[ 
SET @Firstname = AttributeValue("FirstName")
    IF Not Empty(@Firstname) Then
        IF Length(@Firstname) > 8 Then
                SET @Name = "Dear Customer"
        Else
                SET @Name = ConCat('Dear',' ',@Firstname)
        ENDIF
    Else
        SET @Name = "Dear Customer"
    EndIf
]%%

%%=v(@Name)=%%
1
  • AttributeValue() FTW! Dec 14, 2016 at 12:26
0

Please try like this

%%[ 
SET @Firstname = FirstName
  IF Length(@Firstname) > 8 Then
    SET @Name = "Dear Customer"
  ELSE 
    SET @Name = Concat('Dear', @Firstname)
  ENDIF
]%%
%%=v(@Name)=%%
0
0

I would do something like this

%%[
var @message, @name
set @name = FirstName

IF Length(@name) < 8 AND Length(@name) > 0  THEN
set @message = concat(Hi, @name)
ELSE THEN
set @message = "Hi customer!"
ENDIF
]%%

%%=v(@message)=%%
4
  • This is close, but I would be careful with length > 0 as this won't catch true null or empty values every time. Dec 14, 2016 at 13:31
  • @Gortonington Thanks I've noticed that occurring for a lot of my dynamic emails how do you get around it if you actually need to do a value of > 0 I.E you want to display if it's under 1 I guess you could do something like != 1 or > 0.5 right? Dec 22, 2016 at 1:04
  • You can do something like IF NOT EMPTY(@var) AND LENGTH(@var) > 0. This will allow the option to set an else for null or empty variables. There are quite a few options, if you open a question with your specific needs, we may be able to help more Dec 22, 2016 at 2:45
  • @Gortonington Okay excellent thanks for your response. Very nice to know. I'll open another if I run into any issues. Dec 23, 2016 at 0:00
0

@Palash Rai is 100% right - especially about the AttributeValue love. I just would slightly adjust his code to make it more efficient by removing the need for a double IF ELSE statements.

%%[
VAR @Firstname, @Name

SET @Firstname = AttributeValue("FirstName")
SET @Name = "Dear Customer"

IF NOT EMPTY(@FirstName) AND Length(Firstname) > 8 THEN
  SET @Name = CONCAT('Dear ', Firstname)
ENDIF
]%%

This will make sure a default is always set for @Name prior to any conditional processing and would reduce the overall processing and physical code needed.

You must log in to answer this question.

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