Here's what was going on in that quiz last week.
As a reminder, I was wondering if you could figure out this seemingly bizarre combo of languages/contexts:
On a purely letter-of-the-quiz level, it's the Script Token Editor showing (as Pavel P. noted in the comments) Velocity being used to generate JavaScript.
Taken on its own, this isn't a weird application for Velocity. Remember that VTL in its original — and still most common — use is a webpage template language, not an email language. So most VTL is stuff like:
<tr>
#foreach( $price in $prices )
<td>Price ${price}</td>
#end
</tr>
Dynamic webpage stuff (and it's darn good at it). Of course, that table row could just as easily be part of an HTML email, which is why Marketo's choice of Velocity was on point.
In a web environment, if you use Velocity at all, you probably use it to generate entire pages. Hence, somewhere you'd see JS output, too:
<head>
<title>CompanyCo Price Sheet</title>
#foreach( $script_include in $script_includes )
<script id="${script_include.uid}" src="${script_include.src}"></script>
#end
<script>
var my_name = ${web_session.user.name};
</script>
</head>
So again, seeing VTL and JS in the same place is not uncommon. But it is kinda strange to see it in Marketo, innit? Knowing that it will end up in an email, and emails can't run scripts (and will be seen as spammy if they even try).
And then if you look more closely, you'll see that the JS is calling FlowBoost methods. (FBMath is the FlowBoost math shortcut library.) FlowBoost code is executed via Marketo webhooks. But Marketo doesn't call Velocity when executing webhooks, only when rendering emails.
Huh?
The requirements
Let's check the requirements, and you'll see better what's going on.
I have a client who needs to pass Custom Object data to a processing endpoint (doesn't have to be a webhook, but they'd use one if they could). Basically, they maintain the widgets someone has purchased in the past, and they want to make decisions about which one to feature in an email based on the age and number of model(s) owned, purchase price, and other lead characteristics. It's a very simple computation but essential to their personalization effort.
[I'm not a huge fan of COs. Chances are you can do the same or better with a JSON field. But they already built a CO-based setup, so here we are!]
This would be a great fit for a webhook. But Custom Objects can't be passed to 'hooks (they follow a different code path that only allows lead and company tokens). So here I'm sitting on the perfect computation engine but there's no way to get data to it.
Or is there?
Some inspiration
One of my favorite technologies ever is Microsoft's long-obsolete SMTP-based Active Directory replication. Dating from before companies could be expected to have high-speed connections between sites, this feature allowed for replicating a database over SMTP but not actually sending emails proper. In other words, leaning on the fact that SMTP infrastructure is predictable and robust (especially with its built-in store-and-forward + retry logic) then sending custom non-human-readable messages between SMTP servers on the standard port (25). It was seriously brilliant, y'all.
Putting it together
Inspired by the tech above, FlowBoost will now offer something that's, as far as I know, unprecedented: the ability to send executable code to an HTTP webhook endpoint by gatewaying it via SMTP infrastructure.
Here we see ① Custom Objects being turned into JavaScript-readable strings using the in-house Velocity macro #serializeObjectArray, then ② being output as the values of 2 JavaScript variables, and finally ③ being passed to some simple min/max functions.
The resulting JavaScript output looks like:
var vehiclesOwned = [{
"manufacturer" : "Toyota",
"year" : "1989",
"model" : "Camry"
}
,{
"manufacturer" : "Saab",
"year" : "1982",
"model" : "Viggen"
}]
, lead = {
"Email" : "showoff@vtliens.com"
};
return {
Email : lead.Email,
newestVehicleYear : FBMath.max(vehiclesOwned, 'year').year,
oldestVehicleMake : FBMath.min(vehiclesOwned, 'year').manufacturer
}
(In other words, regular valid JS.)
Then it gets sent to a special SMTP gateway in the body of an email. Then it gets executed by FlowBoost. Then it writes back to Marketo.
But how —
Send Alert. And much more interesting stuff on the back end to update Marketo. And that's the (I hope) eye-opening stuff you were looking at!