SOLVED

Re: Velocity script support - Personalisation tokens from Custom Object entries.

Go to solution
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script support - Personalisation tokens from Custom Object entries.

Please use the Syntax Highlighter (“Insert/Edit Code Sample”) when posting code so it’s readable. I edited your post this time.

 

There are some fundamental errors in your code:

1. using the == operator instead of .equals() (as discussed in the past, == can cause hard-to-find bugs because of the way it coerces its operands)

2. incorrect use of #break (the OP didn’t yet indicate that only one open record should be output)

 

Check@Jo_Pitts1’s answer above for some hints and the recommended config-first approach.

SaurabhGoyal_GN
Level 4

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@SanfordWhiteman - Thanks for the suggestions. Made the required changes. 

@PaddyRR - You can remove #break if you want multiple entries in the output. With break statement it will stop on the first encounter. 

 

 #set( $alloutlets = {
			  "Westley Green, Langdon Hills" : {
				"outlet_Name":"Westley Green",
				"outlet_Description":"XXX",
				"outlet_Address":"XXX", 
				"outlet_Brochure":"XXX", 
				"outlet_PageURL":"XXX", 
				"outlet_BookingURL":"XXX", 
				"outlet_HeaderIMG":"XXX"
			  },
			  "Meadow View, Silver End" : {
				"outlet_Name":"Meadow View",
				"outlet_Description":"XXX",
				"outlet_Address":"XXX", 
				"outlet_Brochure":"XXX", 
				"outlet_PageURL":"XXX", 
				"outlet_BookingURL":"XXX", 
				"outlet_HeaderIMG":"XXX"
			  },
			  "The Mulberries, Witham":{
			     "outlet_Name":"The Mulberries",
				 "outlet_Description":"XXX",
				 "outlet_Address":"XXX", 
				 "outlet_Brochure":"XXX", 
				 "outlet_PageURL":"XXX", 
				 "outlet_BookingURL":"XXX", 
				 "outlet_HeaderIMG":"XXX"
			  }
			  }
             )
         #set($enquiry = $sorter.sort(${enquiry_cList}, ["EnquiryDate:desc"]))
         #foreach($enq in $enquiry)
		  #if($enq.EnquiryOutletStatus.equals("Open"))
					#set($aOutlet = $alloutlets[$enq.enquiryOutletName] )
					<table>
					<tr>
					<td>Name - to $aoutlet.outlet_Name</td>
					</tr>
					<tr>
					<td>Description - $aOutlet.outlet_Description</td>
					</tr>
					<tr>
					<td>Location - $aOutlet.outlet_Address</td>
					</tr>
					<tr>
					<td>Brochure - $aOutlet.outlet_Brochure</td>
					</tr>
					</table>
					#break
		  #end
		 #end

 

 
Changed the code in original reply as well. 

Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@SaurabhGoyal_GN ,

You've got a variable called $alloulets at the top (note there is no 't' in the spelling) and you refer to $alloutlets further down (with a 't').  

 

I do like that you have sorted the list to put the most recent enquiry first.

 

That being said, in your sort, you sort $enquiry_clist to $enquiry.  You then iterate your loop with a variable called $enq.  The problem with this is that both $enquiry and $enq are singular and very similar.  You'd do better to sort $enquiry_clist into (for example) $enquiryListSorted.  The variable name for the sorted list clearly states what's gone on and why it exists.

 

Rather than a hard break, I'd be more inclined to use a counter with a max value and a counter set at the beginning e.g.

 

## At the top of your code
#set ($maxOutletsToDisplay = 2)
#set ($outletCounter = 0)
....
....
....
## After an outlet has been displayed
#set ($outletCounter = $math.add($outletCounter ,1))
#if ( $outletCounter.equals($maxOutletsToDisplay) )
  #break
#end

 

 

Without constantly having to change the more delicate code in the output stage, this gives increased flexibility.  You could even use the burger trick to load in a Marketo variable for how many outlets to display and control it without any code changes at all.

 

As always, I've not tested my code so there are likely bugs in it.

 

Cheers

Jo

 

 

Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@PaddyRR ,

Can you check back in and let us know if the solutions proposed are fit for purpose.  There has been quite a lot of effort gone in here.

 

Cheers

Jo

Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@PaddyRR ,

Here is a consolidated version taking the sorted list approach, and a method of controlling the number of open enquiries to display.

 

 

#set ($maxDevelopmentToDisplay = 2)
#set ($displayCounter = 0)

#set( $allDevelopments = {
  "Westley Green, Langdon Hills" : {
    "Development_Name":"Westley Green",
    "Development_Description":"XXX",
    "Development_Address":"XXX", 
    "Development_Brochure":"XXX", 
    "Development_PageURL":"XXX", 
    "Development_BookingURL":"XXX", 
    "Development_HeaderIMG":"XXX"
  },
  "Meadow View, Silver End" : {
    "Development_Name":"Meadow View",
    "Development_Description":"XXX",
    "Development_Address":"XXX", 
    "Development_Brochure":"XXX", 
    "Development_PageURL":"XXX", 
    "Development_BookingURL":"XXX", 
    "Development_HeaderIMG":"XXX"
  },
  "The Mulberries, Witham":{
    "Development_Name":"The Mulberries",
    "Development_Description":"XXX",
    "Development_Address":"XXX", 
    "Development_Brochure":"XXX", 
    "Development_PageURL":"XXX", 
    "Development_BookingURL":"XXX", 
    "Development_HeaderIMG":"XXX"
  }
  }
)

#set($enquiryListSorted = $sorter.sort($enquiry_cList, ["EnquiryDate:desc"]))

#foreach($enquiry in $enquiryListSorted )
  #if($enquiry.EnquiryOutletStatus.equals("Open"))
    #set($aDevelopment = $allDevelopments[$enquiry.enquiryOutletName] )
    <table>
    <tr>
    <td>Welcome to ${aDevelopment.Development_Name}</td>
    </tr>
    <tr>
    <td>${aDevelopment.Development_Description}</td>
    </tr>
    <tr>
    <td>We're located at:<br>${aDevelopment.Development_Address}</td>
    </tr>
    </table>
    #set ($displayCounter = $math.add($displayCounter ,1))
    #if ( $displayCounter.equals($maxDevelopmentToDisplay) )
      #break
    #end
  #end
#end

 

 

 

Let me know if this suits.

 

Cheers

Jo

SaurabhGoyal_GN
Level 4

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@Jo_Pitts1 - Thanks for the suggestions, Got to learn new things. 

PaddyRR
Level 1

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@Jo_Pitts1 @SaurabhGoyal_GN @SanfordWhiteman 

 

Your help is REALLY appreciated with this!!
I am working through this as we speak to test the solution and will report back ASAP.

 

Thanks again!!

PaddyRR
Level 1

Re: Velocity script support - Personalisation tokens from Custom Object entries.

@Jo_Pitts1 @SaurabhGoyal_GN @SanfordWhiteman 

Thanks again for you help with this. 

 

I am struggling to implement the solution and from talking this through with my team, I am not sure if it will be fit for purpose. 

From what I understand the implement your solution the Script would need to be applied very specifically to emails, referencing the email HTML in the code itself. 

 

A big benefit with the original solution as in the code below is that the script and tokens can live in a high-level folder meaning the "Development" tokens can be easily referenced by any email assets within that folder. 

PaddyRR_0-1710496005971.png

I wonder if you know of any way to make this work using something like the below? (P.s. This code still doesn't seem to pull the correct object info into the tokens)

 

#set($enquiry = $sorter.sort(${enquiry_cList}, ["EnquiryDate:desc"]))
#foreach($enq in $enquiry)

#if($enq.enquiryOutletName == "Westley Green, Langdon Hills" && $enq.enquiryOutletStatus == "Open")
        #set ( $Development_Name = "Westley Green") 
        #set ( $Development_Description = "XX.") 
        #set ( $Development_Address = "XX") 
        #set ( $Development_Brochure = "XX") 
        #set ( $Development_PageURL = "XX") 
        #set ( $Development_BookingURL = "XX") 
        #set ( $Development_HeaderIMG = "XX")
       
#elseif ($enq.enquiryOutletName =="Meadow View, Silver End" && $enq.enquiryOutletStatus == "Open")
        #set ( $Development_Name = "Meadow View") 
        #set ( $Development_Description = "XX") 
        #set ( $Development_Address = "XX") 
        #set ( $Development_Brochure = "XX") 
        #set ( $Development_PageURL = "XX") 
        #set ( $Development_BookingURL = "XX") 
        #set ( $Development_HeaderIMG = "XX")        

#elseif ($enq.enquiryOutletName =="The Mulberries, Witham" && $enq.enquiryOutletStatus == "Open")
        #set ( $Development_Name = "The Mulberries") 
        #set ( $Development_Description = "XX") 
        #set ( $Development_Address = "XX") 
        #set ( $Development_Brochure = "XX") 
        #set ( $Development_PageURL = "XX") 
        #set ( $Development_BookingURL = "XX") 
        #set ( $Development_HeaderIMG = "XX")

#end
#end

 

 

 

SaurabhGoyal_GN
Level 4

Re: Velocity script support - Personalisation tokens from Custom Object entries.

Hi @PaddyRR  - As I understand from your reply, You dont want to have any kind of HTML code in the velocity script token so that you can just populate required value in the email using the token. In that case, you will have to create multiple tokens. A new one for a different field. For e.g. 

For outlet_name, you can use this one - This one will give you the Name of the Outlet, where an recent enquiry was made AND having status as "Open"

{{my.outlet_name}}

 

 

#set ($maxDevelopmentToDisplay = 2)
#set ($displayCounter = 0)
#set($enquiryListSorted = $sorter.sort(${enquiry_cList}, ["EnquiryDate:desc"]))
#foreach($enquiry in $enquiryListSorted)
  #if($enquiry.enquiryOutletName.equals("Westley Green, Langdon Hills") && $enquiry.enquiryOutletStatus.equals("Open"))
        #set ( $outlet_Name = "Westley Green")
  #elseif ($enquiry.enquiryOutletName.equals("Meadow View, Silver End") && $enquiry.enquiryOutletStatus.equals("Open"))
        #set ( $outlet_Name = "Meadow View")     
  #elseif ($enquiry.enquiryOutletName.equals("The Mulberries, Witham") && $enquiry.enquiryOutletStatus.equals("Open"))
        #set ( $outlet_Name = "The Mulberries") 
  #end
#set ($displayCounter = $math.add($displayCounter ,1))
  #if ( $displayCounter.equals($maxDevelopmentToDisplay) )
  #break
  #end
#end

$outlet_Name

 

 

 

Similarly, you can create the other tokens for different field values. 

I just wanted to confirm one thing with you, Is this the "Westley Green, Langdon Hills" exact value of the "enquiryOutletName" in CO.

BR,
Saurabh

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script support - Personalisation tokens from Custom Object entries.


I am struggling to implement the solution and from talking this through with my team, I am not sure if it will be fit for purpose. 

From what I understand the implement your solution the Script would need to be applied very specifically to emails, referencing the email HTML in the code itself. 

Not at all. It depends on how you deploy the logic. However, most inexperienced Velocity developers struggle with understanding how to have logic and loops implemented in VTL, while HTML output is both inside and outside VTL tokens. I might suggest you get a more experienced dev on your team.