Capture Specific Type from CO with Velocity Script | Community
Skip to main content
Level 2
September 28, 2021
Solved

Capture Specific Type from CO with Velocity Script

  • September 28, 2021
  • 1 reply
  • 4193 views

Hello,

 

I am working on creating an Email Scripting token that captures the most recent version of a specific Asset Type in a CO. For example, asset type is Type 1, Type 2, Type 3. I need to grab the most recent Type 3; however,

 

When I do the basic "If" statement, it displays nothing because Type 3 is not at the top of the list

 

and 

 

When I do a foreach statement I can just the Type 3, but it displays all of them when I only want the most recent one.

 

Any recommendations on how I can do this?

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 Jo_Pitts1

Thanks for thie @jo_pitts1 - this seemed to have work, but when I try to approve and close it gives me this error and only does that when I included the this related token in the email. Any ideas?

 

Validation Error approving New Owner.New Owner Email —  <div>An error occurred when procesing the email Rendered_Email_Velocity_Error_Area_?! </div> <p>Error invoking method 'get(java.lang.Integer)' in java.util.ArrayList near</p> <div><pre >?</pre></div>

 

Again. I really appreciated your help!


@ameys89771464 ,

you probably need to wrap the whole thing in something like

 

#if( ! $display.alt($AssetList ,"").isEmpty() )

 

 

Which gives you

 

#if( ! $display.alt($AssetList ,"").isEmpty() ) #set( $interestingItems = [] ) #foreach( $item in $AssetList ) #if( $item.Type__c.equals("Registration") ) #set( $void = $interestingItems.add($item) ) #end #end #set( $sortedInterestingItems = $sorter.sort($interestingItems ,"PurchaseDate:desc") ) #set( $mostRecentInterestingItem = $sortedInterestingItems[0] ) Type: ${mostRecentInterestingItem.Type__c} Serial Number: ${mostRecentInterestingItem.SerialNumber} Purchase Date: ${mostRecentInterestingItem.PurchaseDate} End Usage Date: ${mostRecentInterestingItem.UsageEndDate} #end

 

 

Also, you may want to bullet proof this further but ensuring there is at least ONE item in the interestingItems array.  You can do this using .size().  So, that gives you

 

#if( ! $display.alt($AssetList ,"").isEmpty() ) #set( $interestingItems = [] ) #foreach( $item in $AssetList ) #if( $item.Type__c.equals("Registration") ) #set( $void = $interestingItems.add($item) ) #end #end #if ( $interestingItems.size() > 0 ) #set( $sortedInterestingItems = $sorter.sort($interestingItems ,"PurchaseDate:desc") ) #set( $mostRecentInterestingItem = $sortedInterestingItems[0] ) Type: ${mostRecentInterestingItem.Type__c} Serial Number: ${mostRecentInterestingItem.SerialNumber} Purchase Date: ${mostRecentInterestingItem.PurchaseDate} End Usage Date: ${mostRecentInterestingItem.UsageEndDate} #end #end

 

 

Something Important!

As a general rule of thumb, don't presume Velocity code that works in preview will work in the real world (i.e. sending emails).  Make sure and test it and test it again, and then test some more with live data, bad data, lead records with no COs etc etc.  This has been drummed into me (and many others) by @sanfordwhiteman .

 

Cheers

Jo

1 reply

SanfordWhiteman
Level 10
September 28, 2021

You’re going to need to show your code, it‘s not possible to help from merely that description.

 

Please show a full dump of the CO list as well as the code you’re attempting to use to filter it.

Level 2
September 28, 2021

Thanks for your response. Here is the code snippet showing the right type but it is showing all of them.

 

#set( $interestingItems = [] ) #foreach( $item in $AssetList ) #if( $item.Type__c.equals("Registration") ) #set( $void = $interestingItems.add($item) ) Type: ${item.Type__c} Serial Number: ${item.SerialNumber} Purchase Date: ${item.PurchaseDate} End Usage Date: ${item.UsageEndDate} #end #end

 

Output

Type: Registration Serial Number: 22218062 Purchase Date: 2016-03-18 End Usage Date: 2019-03-18 <--- I just want this one

Type: Registration Serial Number: 33150950 Purchase Date: 2007-03-30 End Usage Date: 2010-03-30

SanfordWhiteman
Level 10
September 28, 2021

You’re creating $interestingItems (ArrayList) and adding to it, but then you’re ignoring that variable and outputting all the items!

 

Should be

#set( $interestingItems = [] ) #foreach( $item in $AssetList ) #if( $item.Type__c.equals("Registration") ) #set( $void = $interestingItems.add($item) ) #end #end #foreach( $item in $interestingItems ) Type: ${item.Type__c} Serial Number: ${item.SerialNumber} Purchase Date: ${item.PurchaseDate} End Usage Date: ${item.UsageEndDate} #end