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?
Solved! Go to Solution.
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
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.
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
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
It goes further than that... Amey only wants the registration with the greatest date.
The loop has an if to only display registration types, but no constraint on date.
Yes, it also should be $sorter.sort-ed
Given your if statement is testing for the item type being registration, and BOTH are of type registration, it isn't surprising that are both displaying.
If you want to display the most RECENT registration you'd need to determine the registration with the greatest date.
This could be done by either:
With either approach, you'd then display the registration outside the loop.
Cheers
Jo
Thank you both. I understand the concept and I agree we can take the registration based on published date, but I am a little lost at how to actually grab the first value. Is that different type of command or is that something that needs to be in addition to this?
OK... so it needs to be something like this:
#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}
I've not debugged this, so you'll almost certainly need to tune.
The logic is this:
Now, step 3. technically isn't required - in this instance you could just display from the zeroth array element, but that feels ugly 🙂
Hopefully that gets you going in the right direction.
Cheers
Jo
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!