Re: Using {{program.id}} inside velocity?

Chris_Wilcox
Level 9

Using {{program.id}} inside velocity?

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...

 

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?

14 REPLIES 14
SanfordWhiteman
Level 10 - Community Moderator

Re: Using {{program.id}} inside velocity?

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 9

Re: Using {{program.id}} inside velocity?

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 9

Re: Using {{program.id}} inside velocity?

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...

 

Chris_Wilcox
Level 9

Re: Using {{program.id}} inside velocity?

Even tried putting the script (both wrapped in <script> tags and commented out) in the <body> section, still same thing. The code itself is not rendering when the email goes to compile. I just see two script tags with nothing in between.

<script>
</script>

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Using {{program.id}} inside velocity?

Well the #set wouldn’t render anything. I’ll recheck the code in one of my live instances, it’s been running for a long time and I’m not aware of a regression.

Jep_Castelein2
Level 10

Re: Using {{program.id}} inside velocity?

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 - Community Moderator

Re: Using {{program.id}} inside velocity?


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 - Community Moderator

Re: Using {{program.id}} inside velocity?

@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.

 

 

Chris_Wilcox
Level 9

Re: Using {{program.id}} inside velocity?

It must be because I'm putting my code in the email template, not the unsubscribe language. We have a custom unsub link so getting that unsubscribe language to appear on our emails is actually an issue (even if it's just the comment). 

 

I'll play around some more. At this point, my solution isn't working for other reasons so I might not be able to go down this path anyway.  Thanks for your help as always!