Using {{program.id}} inside velocity? | Community
Skip to main content
Chris_Wilcox
Level 8
September 24, 2021
Question

Using {{program.id}} inside velocity?

  • September 24, 2021
  • 2 replies
  • 6260 views

Having the hardest time figuring out how to access the {{program.id}} value inside of a velocity script token.

 

I found @sanfordwhiteman 's article with the workaround, but having trouble implementing it. 

 

https://blog.teknkl.com/accessing-the-program-id-and-campaign-id-from-velocity-the-new-way-post-june-2019/

 

I put this short script in the <head> my email template (I can't using the unsubscribe HTML section like he suggested), and I can see the value populating correctly in the head.

<script> #set ($MAContext = { "programId" : "{{program.id}}" }) </script>

Now, I'm struggling with how to access that value later in my email inside my script token. 

 

I tried using this, but didn't work. 

${MAContext.programId}

 

Anyone ever done this before?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

SanfordWhiteman
Level 10
September 24, 2021

In my blog post, it’s in comment tags, not a script.

 

But you are accessing it the right way — what do you see when you output just

${MAContext}
Chris_Wilcox
Level 8
September 24, 2021

It just output's the text "${MAContext}" when I output

${MAContext}

 

I realize you had it as a comment, but since I had to move the code to my email template, I wrapped it in <script> tags instead. 

Chris_Wilcox
Level 8
September 24, 2021

One update...

 

I looked at the source code of my delivered email using the script, and it looks like the value is not appearing in the code. I tried with the script wrapped both in the <script> tags, as well as adding it between comment tags. In both instances, none of the code is present in the header. But if I add the script to body of an email, seems to work. 

 

Going to try a few more things...

 

Jep_Castelein2
Level 10
September 24, 2021

It's my experience that if you assign the Program ID to a Velocity variable in the email itself, it's not available in the context of Email Script Tokens and vice versa. However, as long as you don't need Custom Object or Opportunity Data, you can just write your entire Email Script in the Email Template. Surprisingly enough, it doesn't have to be in an Email Script token. 

In Sanford's blog post, he puts it in the "Unsubscribe HTML" area. I have never tested whether Velocity variables defined there are indeed available in Email Script Tokens. 

SanfordWhiteman
Level 10
September 24, 2021

In Sanford's blog post, he puts it in the "Unsubscribe HTML" area. I have never tested whether Velocity variables defined there are indeed available in Email Script Tokens. 

They are (or were at the time the post was published). The goal is to transform {{program.tokens}} into Velocity ${references} by knowing there’s a common context where both are available

 

There’s actually yet another way to do it, but it’s even more experimental so don’t want to go there now.

SanfordWhiteman
Level 10
September 25, 2021

@chris_wilcox I just tested the code in that post in a new instance and it still works fine. Don't know what else you could be doing differently.