SOLVED

Velocity: Multiple name in First Name

Go to solution
Vidhi_Kare
Level 3

Hi all,

Hope you are doing well.

I am trying to solve one problem using Velocity to Display " Hi Member" instead of Name  in First Name field when it consists of multiple names. 

Example : Rodney William Hydes

I was thinking to use length check but we don't know the max chars that can be input in this field. So if it is just one input and length is more than 2 then I have used the code to display the name by taking the index at 0.

However, it seems like I would need another condition when length is greater than 2 and then find a solution to display Hi Member instead of name if its not in one string.

Any inputs will be appreciated. Hope it is not confusing.

 

Kind Regards,

Vidhi

 

Tags (1)
1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator
Just split on space and check the size of the resulting array.

View solution in original post

21 REPLIES 21
SanfordWhiteman
Level 10 - Community Moderator

Why are you referring to character length when what you mean is number of words?

Vidhi_Kare
Level 3

Hi Sanford, 

 

Thank you for your reply.

Yes I meant number of words & in this case, all I am concerned about multiple strings in one field.

 

Kind Regards,

Vidhi

 

SanfordWhiteman
Level 10 - Community Moderator
Just split on space and check the size of the resulting array.
Vidhi_Kare
Level 3

Hi @SanfordWhiteman ,

Thank you for your inputs.

I tried that but then I thought I am anyways going to ignore everything  & display 'Hi Member' if First Name array will have space even once in name and its not one continuous string with size more than 3. Hence I used the logic as below:

#elseif (${lead.FirstName.length()} <= 2 || ${lead.FirstName.contains(" ")})
Hi Member
#elseif (${lead.FirstName.length()} > 2 )
$display.capitalize(${lead.FirstName})

 

It is working in my case.

 

Kind Regards,

Vidhi

Jo_Pitts1
Level 10 - Community Advisor

@Vidhi_Kare ,

Stepping away from the technical aspects of what you're doing, and into the softer 'personalisation' side.....

The problem with your approach to showing 'Hi Member' for someone with two characters in their name  is that people have two letter names.

Me for example.

Ty Cobb, Al Sharpton, Ed Asner being others. 

Why presume that a two character name is invalid?

 

 

Vidhi_Kare
Level 3

Hi @Jo_Pitts1 ,

CC : @SanfordWhiteman 

 

It is a really good point actually. So, in our database, we have noticed names like Aa, Ab, Bb etc so which is why we thought to display 'Hi Member'  But yeah there could be a missed opportunity for good data as well like you mentioned. 

So, I am thinking to add a condition of comparing first two chars using substring method and if it is same then display 'Hi Member' else display their name.

 

Kind Regards,

Vidhi

 

SanfordWhiteman
Level 10 - Community Moderator

So, I am thinking to add a condition of comparing first two chars using substring


Use a regex

#if( $lead.field.matches("(?i)(.)\1") )

 

Vidhi_Kare
Level 3

Hi @Jo_Pitts1 & @SanfordWhiteman 

 

Thank you for all your inputs.

I just had one more thought. Is there a way to capture Subject line text in a token and then capitalize the first word of the subject line if it is not.

We want to display <Name>, < Lower case of first word in Subject Line Text> in case of happy world scenarios for us.

else,

we want to display just the subject line with first word capitalized.

Example:

Name is Ed

Ed, our latest news and articles

Name is : A

Our latest news and articles

How do we capture the subject line value if we can using velocity or any other method.

There is an alternative solution but with that if someone is using that token then they will have to tweak the token to add the capitalized subject line in that condition. That can result in error if that user is not aware of scripting, which is why trying to find the automate solution.

 

Kind Regards,

Vidhi

 

 

 

 

Phillip_Wild
Level 10

It's a new token but the logic is effectively the same, if I understand correctly.

 

The output would be different, but the logic to check whether it's a valid name is the same. Just make sure your entire subject line is in that token - don't have the token only output the name, then have the rest as free text. 

Vidhi_Kare
Level 3

HI @Phillip_Wild ,

 

Actually like @SanfordWhiteman  assisted with his inputs about the subject line rendering, I believe my case falls in that category.

I understand your point but I was trying to automate the first word of the subject line free text by reading the text via any means in script and in case it is not a valid name, the token will display blank( which is fine) however, I wanted that the free text's first word will also be capitalized.

But since, we cannot parse the subject line data once the body is being parsed, I am afraid we can do this automation with scripting.

 

Kind Regards,

Vidhi

Jo_Pitts1
Level 10 - Community Advisor

@Vidhi_Kare ,

you can include the same logic around first name in your subject like token.

 

There are probably better ways of doing this, but this will work.

#set ($subjectLine = 'this is my subject line')
#set ($firstName = ${lead.FirstName.toLowerCase()})

#if( $firstName.matches("(?i)(.)\1") )
$display.capitalize($subjectLine)
#else
$display.capitalize($firstName), ${subjectLine}
#end

 

So, I guess you can't just have someone enter in a subject line, and retroactively change it, but you can have a token containing the subject line and get the outcome you want.

 

I've tested this a bit further, and you can separate it into two tokens, which gets you closer to where you want to be.

Set the subject line:

Put a token containing this at the top of your email body.  It is easy for non technical people to edit, and could even be extended to look at segment data etc. to create a dynamic subject line

#set ($subjectLine = 'this is my subject line')

 

Emit the subject line:

place a token containing this code in your subject line

#set ($firstName = ${lead.FirstName.toLowerCase()})

#if( $firstName.matches("(?i)(.)\1") )
$display.capitalize($subjectLine)
#else
$display.capitalize($firstName), ${subjectLine}
#end

 

There is something to learn from that BTW - Marketo processes tokens in the email body BEFORE it processes tokens in the subject line.  If it did it the other way around (subject before body), the separated approach wouldn't work.

 

I think that is as close as you're going to get.

 

Cheers

Jo

SanfordWhiteman
Level 10 - Community Moderator
Guess that’s the thing, though, the Subject is expected to not contain a token. Which won’t work.
Jo_Pitts1
Level 10 - Community Advisor

@SanfordWhiteman ,

to completely over complicate this, it becomes an interesting use case for template variables in Velocity 🙂

You could set a global template string variable called something like mktoSubjectLine (no code editing required to control it).

Read the global variable into Velocity using your black magic trick

Drop my code into the subject line and tada

 

So, working on the theory we have a global variable called mktoSubjectLine set on the template, that can readily be set by anyone with zero technical skills, we then create two tokens

setASubjectStart

 

#define( $subjectLine )

 

 

setASubjectEnd

 

#end

 

 

In the Email

Place this somewhere in the email body

 

{{my.setASubjectStart}}${mktoSubjectLine}{{my.setASubjectEnd}}

 

 

And continue to use the emit a subject line code from my earlier post. 

 

The subject line is now free-text, just not in the actual subject line 🙂

 

Talk about an over engineered solution!!!

Jo_Pitts1
Level 10 - Community Advisor

@Vidhi_Kare ,

to take my slightly bonkers solution even further, you can place ALL of this in the subject line:

 

 

{{my.setASubjectStart}}this is my super cool subject{{my.setASubjectEnd}}{{my.emitASubject}}

 

where my.emitASubject is this code:

 

#set ($firstName = ${lead.FirstName.toLowerCase()})
#if( $firstName.matches("(?i)(.)\1") )
$display.capitalize($subjectLine)
#else
$display.capitalize($firstName), ${subjectLine}
#end

 

Your team can then just edit the subject line BETWEEN the first two tokens.

 

Technically, it functions the same way as the solution using a mkto template variable, but it places the output for the subject line inline in the Subject Line, with no variable required.

 

I believe that is as close as I can get you to nirvana!

 

Cheers

Jo 

SanfordWhiteman
Level 10 - Community Moderator

You would have to have a Velocity token in the Subject. There's no way to backtrack and change Subject data once the body is being parsed.

SanfordWhiteman
Level 10 - Community Moderator

Agreed, 2-character names are quite possible... never seen evidence that 2-character false names are more common than 2-character real names.

SanfordWhiteman
Level 10 - Community Moderator

OK. You must remove those curly braces as they are not appropriate inside #directives (they break function chaining)

Vidhi_Kare
Level 3

Hi @SanfordWhiteman ,

 

Thank you for catching it.

Yes, actually I have used a variable to store the value. I just forgot to use it in my code.

 

Kind Regards,

Vidhi

Jo_Pitts1
Level 10 - Community Advisor

@SanfordWhiteman ,

is there a reason you didn't suggest using .contains(" ")?

I have a vague hunch it is because you're giving the OP a solution that allows them to then use the zeroth element of the array as the name to display, but there are probably many other technical reasons why your approach is better.

Cheers

Jo

SanfordWhiteman
Level 10 - Community Moderator

No particular reason except that it lets you choose whether to allow 2-part names but not 3-part names.