Selecting a particular Custom object to display in Velocity Token for emails

LCENTENO
Level 3

Selecting a particular Custom object to display in Velocity Token for emails

Hi everyone, 
Quick question. I have a custom object called "Accounts", which has several different data fed fields including one called "Product". I created the custom tokens in my velocity script (snapshots below) to show the balance, and next date due of a specific product.


Payment Velocity Script:

LCENTENO_1-1682610442284.png

Next Due Date Velocity Script:

LCENTENO_2-1682611018262.png

Product Code Velocity Script:

LCENTENO_3-1682612187074.png

 

However, the data shown when the tokens are placed in the email is incorrect, as it is pulling the payment amount and due date info for the wrong product. The issue here is that under the "Accounts" custom object, another entry is added for every new product that is purchased by a customer. My question is, is there a string of code that should be applied to the "payment" and or "due date" custom tokens to ensure that the data pulled is for the correct Product? Thanks in advance. ๐Ÿ™‚

Best,

Lucas

42 REPLIES 42
Darshil_Shah1
Level 10 - Community Advisor

Re: Selecting a particular Custom object to display in Velocity Token for emails

You'd need to first sort the custom object records based on the field of interest (e.g., by last updated datetime). By default, the custom object records are sorted by created datetime in descending order. You should check out Sandy's blog on Sorting objects and lists in Velocity. Additionally, if you're looking to grab the custom object based on a field's value, then you can loop through the custom object records, and use conditionals to determine whether the data in the CO record matches the data you're looking for (sample code below).

 

#foreach($data in $cCUAccount_cList)
#if($data.productCode.equals("123"))
${data.currentPayment}
#end
#end

 

You can use the $TriggerObject (e.g., $TriggerObject.productCode) in case you want to reference the CO that triggered the campaign.

 

LCENTENO
Level 3

Re: Selecting a particular Custom object to display in Velocity Token for emails

Hi Darshil,
So, two things. First to confirm, I would need to apply the following below to both my Payment and Due Date Tokens correct?
Next Due Date Example:

${cCUAccount_cList.get(0).nextDueDate}
#foreach($data in $cCUAccount_cList)
#if($data.productCode.equals("123"))
${data.nextDueDate}
#end



Second, since we have 15 different product codes, how would that be applied to the code so that the custom token picks up and applies the correct Next Due Date and or Payment? Thanks again for your insight and patience. ๐Ÿ™‚

Best,
Lucas

Darshil_Shah1
Level 10 - Community Advisor

Re: Selecting a particular Custom object to display in Velocity Token for emails

Why are you creating separate email script tokens for displaying each custom object attribute? You can just create a single email script token, add a conditional statement based on the product code you want to grab the CO record for and print all the corresponding CO record fields. Sample script below: 

 

#foreach($data in $cCUAccount_cList)
#if($data.productCode.equals("123"))
${data.nextDueDate}
${data.payment}
${data.productCode}
#end
#end

 

Also, I don't understand why are you referencing the first element of the CO for printing the "nextDueDate" data (i.e., in the first line of your code). Do you really need it? As I said in my previous comment, by default the CO records are stored by created date, in descending order. So the first record at the 0th index would be the one that was created at last. Let us know if you have questions.

 

LCENTENO
Level 3

Re: Selecting a particular Custom object to display in Velocity Token for emails

Hi Darshil,
The email copy we are applying the tokens to is below:
{{lead.First Name:default=edit me}}, this is a reminder that your first loan payment of {{my.CurrentPayment:default=edit me}} is due on {{my.NextDueDate:default=edit me}}
So we want to pull the value for each token based on the Product Code. So, in the snapshot, we have a member who has the NEW AUTO CE12 Product Code, so his payment of 54 is due on December 15th. 

LCENTENO_1-1682623903908.png

How would this be implemented into the email copy above for payment and next due date tokens?
Best,

Lucas

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Selecting a particular Custom object to display in Velocity Token for emails


{{lead.First Name:default=edit me}}, this is a reminder that your first loan payment of {{my.CurrentPayment:default=edit me}} is due on {{my.NextDueDate:default=edit me}}.

With Velocity tokens, you donโ€™t use the :default= syntax. You choose the default output within your code and only put {{my.token}} in the email.

SanfordWhiteman
Level 10 - Community Moderator

Re: Selecting a particular Custom object to display in Velocity Token for emails

Thereโ€™s not enough information for a complete answer yet.

 

Are you sending this at the moment they have the CO record added? If so, trigger on Added to <Custom Object Name> and use $TriggerObject in your code. (Donโ€™t iterate over the list of CO records, itโ€™s unnecessary because you already know which record youโ€™re focused on.)

 

With output this brief, you can output it all from 1 {{my.token}}, which is what Darshil mentioned above. Just include the whole paragraph in the token. Or you could have the 2 {{my.token}}s as in your example. But donโ€™t repeat the same code across tokens, that leads to hard-to-find bugs and wastes resources.

Darshil_Shah1
Level 10 - Community Advisor

Re: Selecting a particular Custom object to display in Velocity Token for emails

Agreed with Sandy here! There's just not enough information here to provide you with a set-in-stone code / decide whether you should be iterating over the list of CO records or just could get away with the TriggerObject to pull fields of the record that triggered the campaign. I've tried including both the options in my first comment, but the one that you should actually implement depends on your use-case.

 

LCENTENO
Level 3

Re: Selecting a particular Custom object to display in Velocity Token for emails

Hi Darshil and Sandford,

We are basically targeting this towards customers who opened a new loan. We wait until 7 days before their first payment is due before sending the message indicated above. 

Also, for the product code, since there are more than one product code, could you install all the product categories in the same code as listed by Darshil, using the dummy example product codes AUTO222, AUTO225, AUTO2277:

#foreach($data in $cCUAccount_cList)
#if($data.productCode.equals("123"))
${data.nextDueDate}
${data.payment}
${data.productCode}
#end
#end

Would you be using the && and || code to separate each product code in the #if statement? 

Thanks in advance and let me know if this helped to clarify or if more info is needed.

Best,

Lucas

SanfordWhiteman
Level 10 - Community Moderator

Re: Selecting a particular Custom object to display in Velocity Token for emails

Please edit your post to use the Syntax Highlighter, then weโ€™ll continue. Thanks.