Accessing the Program ID and Campaign ID from Velocity, the new way (post-June 2019)

If you’re shocked by the method below, you shouldn’t be, because everything in Marketo is driven by Velocity (even if you’ve never created a single Email Script token, the whole email assembly process uses Velocity).

In June 2019, Marketo moved the helpful HashMap called $program out of the Velocity context. Perhaps an oversight, perhaps not. (That release broke enough useful things that I chose not to push back… and end up with more things accidentally broken!)

But it’s always been possible to get that data another way.

Secret weapon alert: the Unsubscribe HTML — like several other things that aren’t labeled “code” but are evaluated as code – can contain Velocity. And more exciting, it can contain regular tokens like {{Program.Id}} and {{Campaign.Id}} as well.

So you can add an HTML comment (make sure all your Admins know why it’s there, so it doesn’t get removed) like so:


This creates the Velocity object $MAContext which you can use elsewhere in Velocity. Many other things are possible here too, I won’t get into them today.

P.S. Keep it simple, and take care that your VTL can’t throw any errors. Note that I put quotation marks around the token values, because {{Campaign.Id}} doesn’t always have a value (an email doesn’t have to belong to a Campaign, remember) and if it’s empty and unquoted, you have a fatal syntax error:

#set( $object = {
  "programId" : 1234,
  "campaignId" : // <--