SOLVED

Email Script Token Shows Token as Text

Go to solution
abrarkhandoker
Level 1

Email Script Token Shows Token as Text

I'm sure this is a simple solution but, being as new to Velocity as I am (very), I have no idea how to resolve it.

 

I created my script (below) and previewed the email. It results in nothing. If I create a standalone display of one of the in-script tokens, it results in the token as text. For example, having it display "$eventCount" results in the literal text of "$eventCount".

 

Am I missing something?

 

#set($originalLength = $lead.Field-1.length())
#set($replacedLength = $lead.Field-1.replace(";","").length())
#set($eventCount = $originalLength - $replacedLength)

#if($eventCount == 1) <p>You succesfully registered for a training class. If you are attending virtually, you will recieve the link to join via this email address the day before training begins. If you have any questions please reach out to Person at <a href="mailto:person@emailaddress.com" target="_blank"> person@emailaddress.com</a>.</p>
	#else <p>You succesfully registered for $eventCount Trend Micro training classes. If you are attending virtually, you will recieve the links to join via this email address the day before training begins. If you have any questions please reach out to Person at <a href="mailto: person@emailaddress.com" target="_blank"> person@emailaddress.com</a>.</p>
#end

<ul>
#if ($lead.Field-1.matches(".*Product1.*")) <li><strong>Product 1 Certified Professional</strong> DATE<br /><a href="LINK.COM" target="_blank">Add to Calendar&nbsp;&rarr;</a></li> #end
#if ($lead.Field-1.matches(".*Product2.*")) <li><strong>Product 2 Certified Professional</strong> DATE<br /><a href="LINK.COM" target="_blank">Add to Calendar&nbsp;&rarr;</a></li> #end
#if ($lead.Field-1.matches(".*Product3.*")) <li><strong>Product 3 Certified Professional</strong> DATE<br /><a href="LINK.COM" target="_blank">Add to Calendar&nbsp;&rarr;</a></li> #end
</ul>
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Shows Token as Text

Let's step back. What business logic are you trying to implement here? And field-1 is not a valid dot-property name; Marketo will never generate such a name.

View solution in original post

9 REPLIES 9
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Shows Token as Text

Let's step back. What business logic are you trying to implement here? And field-1 is not a valid dot-property name; Marketo will never generate such a name.

abrarkhandoker
Level 1

Re: Email Script Token Shows Token as Text

Ah, you're right. Not my smartest moment. I typed the field names with dashes and underscores as if I were within the email itself instead of as alphanumeric only. Thanks for the catch, Sanford!

 

Regarding the use case, it's to change a confirmation email's copy based on which events (1 or more) a recipient signs up for as opposed to create a permutation of confirmation emails based on all the combinations of event registrations.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Shows Token as Text

Still not seeing the full story. Pull back a bit. What is exact format of the field you’re doing that weird replace/regex on? Surely there’s a better way.

Jo_Pitts1
Level 9

Re: Email Script Token Shows Token as Text

@SanfordWhiteman ,

my best guess is that @abrarkhandoker has a ; delimited list of registrations, that always ends in ;

By replacing the ; with '' and subtracting the two lengths, he's able to determine the number of events (a delta of 1 means one event). YUCKY!!!!!!

 

The section of matching *product-x* is to then generate each event output for each event that a person has registered for.  Also yucky!

 

@abrarkhandoker why not convert the ; delimited event list into an array and use the count of the array to determine  behaviour.   You can then iterate the array to generate your final stage output as well.  I've also placed the final stage output into a key map for scalability and separation (i.e., if you end up with 20 events - your approach becomes unmaintainable).

 

#set( $allEventDetails = {
  "Product 1" : {
    "blurb":"this some stuff about product 1 registration",
    "date":"Wednesday, 11 November 2022",
    "time":"10:30am",
  },
  "Product 2" : {
    "blurb":"Wow - you're interesting in product two..wahoo",
    "date":"Wednesday, 18 November 2022",
    "time":"10:30am",
  },
  "Product 3" : {
    "blurb":"So much threeness.  It's got to be good for you",
    "date":"Wednesday, 18 November 2022",
    "time":"10:30am",
  }
}
)
#set( $eventRegistrations = $lead.Field1.split("[;]") )

#if( $eventRegistrations.size().equals(1) )
  Output some stuff
#else
  Output some other stuff
#end

#foreach( $code in $eventRegistrations )
  #set( $aEvent = $allEventDetails[$code] ) 
  ## maybe wrap a nice table around this stuff
  <p>${aEvent.blurb}</p>
  <p>${aEvent.date}</p>
  <p>${aEvent.time}</p>
#end

 

 

I've written several times on the notion of 'set' and 'emit' tokens as well which helps simplify things.  Do all the setting of your output variables in a set token, and then do the output in the emit tokens (the classic notion of separating data and presentation).  In this case, your set token would be everything down to (and including) the line that does the split.  The other two sections (the if and the foreach) are discrete and can be placed in their own emit tokens which is handy if you want other content between the initial output and the looped output.

 

My standard caveats apply: 

  1. I've not tested nor debugged this.
  2. @SanfordWhiteman can probably do it better.

Cheers

Jo

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Shows Token as Text

Sure, that looks good. Don’t need the brackets around the semicolon though.

abrarkhandoker
Level 1

Re: Email Script Token Shows Token as Text

Interesting. Thank you both! My "coding" background comes from Excel and VBA where my back-alley method of finding the number of times a character shows up is to find the delta the way Jo mentioned so I just translated it over here. Jo's understanding of the use-case is spot-on.

 

This is all amazing and really insightful. I'm definitely going to try to learn Velocity better. I've searched a lot but, I have trouble finding any useful documentation for a beginner to learn about Velocity in an effective way. For background, I learned almost all of my Excel and VBA through no-idea-what-is-happening googling, learn-by-breaking and brute-force trial-and-error. Is there a nice repository of Velocity-related information I can reference instead of annoying you two with simple questions?

Jo_Pitts1
Level 9

Re: Email Script Token Shows Token as Text

@abrarkhandoker ,

this is Sanford's blog.  It contains a lot of Marketo stuff in general, and a lot of velocity stuff as well.

https://blog.teknkl.com/

 

Failing that, keep asking questions.  It is how I learn best.

 

Cheers

Jo

Jo_Pitts1
Level 9

Re: Email Script Token Shows Token as Text

@SanfordWhiteman ,

interesting about not needing the [] around the ; .

 

I based that on code you originally helped me with that did have the [].  Not sure why that is the case?

 

Cheers

Jo

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Shows Token as Text

No reason to have it be in a character class if it’s the only thing you’re splitting on.