Velocity Script- How do I get the data from most recent SFDC Custom Object record? | Community
Skip to main content
Malay_Chakrabo1
New Participant
July 10, 2020
Solved

Velocity Script- How do I get the data from most recent SFDC Custom Object record?

  • July 10, 2020
  • 1 reply
  • 5150 views

Hi there, 

 

I am trying to get various Velocity script to fetch data from a Subscription SFDC Custom Object and I wanted to ask help in understanding how do I make sure that I am selecting the relevant Subscription record. I am trying to only select the records whose subscription is ending in the next 3 months. 

 

Thus, I wanted to check if there is a way in VTL to filter/constraint certain conditions? So the script always picks up the relevant data. ?

 

Example Script: 

 

##Fetch Subscription Data from SFDC Custom object #set($SubscriptionName = ${Subscription__cList.get(0).Name}) #set($SubscriptionStatus = ${Subscription__cList.get(0).Subscription_Status__c}) #set($AutoRenew = ${Subscription__cList.get(0).AutoRenew__c}) #set($NextRenewalDate = ${Subscription__cList.get(0).NextRenewalDate__c}) ##Conditions #if (($SubscriptionStatus == "Active") && ($AutoRenew == "true")) ${SubscriptionName} #else Test Fallback Name #end

 

With this script where there are multiple subscription records, the script is picking up the oldest record always and the fallback sentence is displayed. 

 

Please let me know if you know how I can select the right record to fetch data from? 

 

Thanks

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

First, let's touch up your code to use simpler syntax and to also use .equals() instead of == (the == operator is broken in obscure ways and should be avoided so you don't have hard-to-find bugs).

##Fetch Subscription Data from SFDC Custom object #set( $SubscriptionName = $Subscription__cList.get(0).Name ) #set( $SubscriptionStatus = $Subscription__cList.get(0).Subscription_Status__c ) #set( $AutoRenew = $Subscription__cList.get(0).AutoRenew__c ) #set( $NextRenewalDate = $Subscription__cList.get(0).NextRenewalDate__c ) ##Conditions #if ( $SubscriptionStatus.equals("Active") && $AutoRenew.equals("true") ) ${SubscriptionName} #else Test Fallback Name #end

 

Now, let's think about the logic. You're fetching the first item (.get(0), which is the same as [0] by the way) in the list of objects.

 

That won't work: you need to iterate over the list and keep only the items you want. There's no explicit collection filtering mechanism. You #foreach over the list and then pull out the ones you want. For example:

#set( $interestingItems = [] ) #foreach( $item in $originalItem__cList ) #if( $item.field1.equals("interestingValue") ) #set( $void = $interestingItems.add($item) ) #end #end

 

Then your smaller list $interestingItems holds the ones you pulled out.

 

You'll also want to read my seminal post on date/datetime math in VTL:

 

https://blog.teknkl.com/velocity-days-and-weeks/

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
July 10, 2020

First, let's touch up your code to use simpler syntax and to also use .equals() instead of == (the == operator is broken in obscure ways and should be avoided so you don't have hard-to-find bugs).

##Fetch Subscription Data from SFDC Custom object #set( $SubscriptionName = $Subscription__cList.get(0).Name ) #set( $SubscriptionStatus = $Subscription__cList.get(0).Subscription_Status__c ) #set( $AutoRenew = $Subscription__cList.get(0).AutoRenew__c ) #set( $NextRenewalDate = $Subscription__cList.get(0).NextRenewalDate__c ) ##Conditions #if ( $SubscriptionStatus.equals("Active") && $AutoRenew.equals("true") ) ${SubscriptionName} #else Test Fallback Name #end

 

Now, let's think about the logic. You're fetching the first item (.get(0), which is the same as [0] by the way) in the list of objects.

 

That won't work: you need to iterate over the list and keep only the items you want. There's no explicit collection filtering mechanism. You #foreach over the list and then pull out the ones you want. For example:

#set( $interestingItems = [] ) #foreach( $item in $originalItem__cList ) #if( $item.field1.equals("interestingValue") ) #set( $void = $interestingItems.add($item) ) #end #end

 

Then your smaller list $interestingItems holds the ones you pulled out.

 

You'll also want to read my seminal post on date/datetime math in VTL:

 

https://blog.teknkl.com/velocity-days-and-weeks/

Mike_St_Louis
New Participant
July 14, 2020

Hey Sanford - 
So using your methodology - we were able to get the script below to produce the results from the SFDC Subscription Object that we needed, so thank you for that...

 

#set( $interestingItems = [] ) #foreach( $item in $Subscription__cList ) #if( $item.Subscription_Status__c.equals("Active") ) #if( $item.AutoRenew__c.equals("1") ) #if( $calNow.compareTo($renewSubDate) < 90) #set( $void = $interestingItems.add($item) )

 

But how would we go about turning the results like this below into a listed format by just "Subscription Name" to appear in the token in the email?

 

{Name=A-S00013411, Subscription_Status__c=Active, AutoRenew__c=1, NextRenewalDate__c=2020-09-21, SubscriptionEndDate__c=2020-09-21} {Name=A-S00013675, Subscription_Status__c=Active, AutoRenew__c=1, NextRenewalDate__c=2020-10-05, SubscriptionEndDate__c=2020-10-05}

 

 

SanfordWhiteman
New Participant
July 14, 2020
${display.list($interestingItems,"<br>","<br>","Name")}