SOLVED

Re: Capture Specific Type from CO with Velocity Script

Go to solution
Jo_Pitts1
Level 10 - Community Advisor

Re: Capture Specific Type from CO with Velocity Script

@Amey_Shivapurka ,

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

SanfordWhiteman
Level 10 - Community Moderator

Re: Capture Specific Type from CO with Velocity Script

You don’t need the null coalesce in this case, just

#if( !$AssetList.isEmpty() )

will do.

 

Amey_Shivapurka
Level 2

Re: Capture Specific Type from CO with Velocity Script

Thank you both this worked.