email script token not working

Anonymous
Not applicable

Hi friends,

I'm writing a script token that removes the domain extension (like ".com" or ."org") of an email address before displaying the result whenever called from a flow.

It's not working. Perhaps I've done something wrong here. Please can anyone help?

set ($leadEmail=${lead.Email})   

set ($fiveLetterEnding = [".co.uk", ".co.jp", ".co.za"])

set ($fourLetterEnding = [".info", ".name", ".mobi"])

set ($threeLetterEnding = [".com", ".net", ".edu", ".gov", ".org", ".biz"])

set ($twoLetterEnding = [".fr", ".be"])

If ($leadEmail.matches("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$"))

  foreach ($fiveLetterEnding)

    if ($leadEmail.matches("^$fiveLetterEnding"))

    $leadEmail=$leadEmail.substring(0, 4)

    end

  end

  foreach ($fourLetterEnding)

    if ($leadEmail.matches("^$fourLetterEnding"))

    $leadEmail=$leadEmail.substring(0, 3)

    end

  end

  foreach($threeLetterEnding)

    if ($leadEmail.matches("^$threeLetterEnding"))

    $leadEmail=$leadEmail.substring(0, 2)

    end

  end

  foreach($twoLetterEnding)

    if ($leadEmail.matches("^$twoLetterEnding"))

    $leadEmail=$leadEmail.substring(0, 1)

    end

  end

 

end

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Need more than "not working" -- actual behavior and errors.  And definitely note Greg's follow-up.

I'll say straight away that what you've pasted isn't valid Velocity (VTL) code, since the lines don't begin with '#'.  (The way you're doing the parsing is also way too verbose -- if you're hard-coding the small list of domains you care about, you only need one line to trim to trim all such values.)

Anonymous
Not applicable

error_message.jpg

Anonymous
Not applicable

Thanks Sanford, I think i'm still doing something wrongly. Please would you know any ways of making this code simpler and less verbose?

Grégoire_Miche2
Level 10

HI Kenneth,

what do you mean by:

"before displaying the result whenever called from a flow"

?

Velocity tokens only render in emails. If you were trying to use it as a new value in a flow step, for instance, it would not work.

-Greg

Anonymous
Not applicable

I'm very sorry for my late reply.

Actually, I wanted to build a simple campaign that assigns company names to leads that have no company data but have an email address with their company's official domain like "......@microsoft.com".

So the aim is to trim off "......@" and ".com" to have just "microsoft" and in the flow of this campaign, I plan to call an email script token like "my.companyName" so that I have Company Name = "my.companyName".

The script above was supposed to ronly remove ".com" from the email address but didn't work. Perhaps I am doing something wrong

SanfordWhiteman
Level 10 - Community Moderator

Actually, I wanted to build a simple campaign that assigns company names to leads that have no company data but have an email address with their company's official domain like "......@microsoft.com".

Email scripts can't update lead fields. They are used to display custom content in an emails, but they do not write content back to the database.

Anonymous
Not applicable

Thank you Sanford. I was initially thinking of downloading company names of leads and trimming off the unwanted strings with excel. Perhaps I'll just do that

SanfordWhiteman
Level 10 - Community Moderator

If you have a distinct set of string matches that you're interested in, a FlowBoost script can do this easily:

var PUBLIC_SUFFIXES = [".co.uk", ".co.jp", ".co.za", ".info", ".name", ".mobi", ".com",

  ".net", ".edu", ".gov", ".org", ".biz", ".fr", ".be"],

   COMPANY_MAPPINGS = {

     "microsoft, ms": "Microsoft Corporation",

     "fortinet, fnet": "Fortinet",

     "cisco, linksys": "Cisco Systems"

   },

  leadEmail = {{Lead.Email Address}}

  emailInferredCompany = getInferredCompany(leadEmail);

function getInferredCompany(email) {

   var RE_PUBLIC_SUFFIXES = new RegExp('(@|\\.)(\\w*)(' + PUBLIC_SUFFIXES.map(reEscDomain).join('|') + ')$', 'i'),

     suffixMatch = (leadEmail.match(RE_PUBLIC_SUFFIXES) || [])[2],

     companyMatch;

   if (suffixMatch) {

     Object.keys(COMPANY_MAPPINGS).forEach(function(mappingKey) {

       var RE_COMPANY_MAPPING = new RegExp('^(' + simpleListToArray(mappingKey).join('|').trim() + ')$', 'i'),

         mappingValue = COMPANY_MAPPINGS[mappingKey];

       if (suffixMatch.match(RE_COMPANY_MAPPING)) {

         companyMatch = companyMatch || suffixMatch.replace(RE_COMPANY_MAPPING, mappingValue);

       }

     });

   }

   return companyMatch || "";

}

function reEscDomain(dnsDomain) {

   return dnsDomain.split('.').join('\\.');

}

function simpleListToArray(list) {

   return list.split(/\s*,\s*/).reduce(function findEscCommas(prev, next) {

     if (prev.length && prev[prev.length - 1].match(/\\$/)) {

       return prev.concat(prev.pop().replace(/\\$/, ',' + next));

     } else {

       return prev.concat(next);

     }

   }, []);

}

But I would caution that this approach is going to have a bunch of false positives.  I have a @microsoft.com address, for example, but I don't work for Microsoft.