SOLVED

Re: Capture Specific Type from CO with Velocity Script

Go to solution
Amey_Shivapurka
Level 2

Capture Specific Type from CO with Velocity Script

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?

1 ACCEPTED SOLUTION

Accepted Solutions
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

View solution in original post

12 REPLIES 12
SanfordWhiteman
Level 10 - Community Moderator

Re: Capture Specific Type from CO with Velocity Script

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.

Amey_Shivapurka
Level 2

Re: Capture Specific Type from CO with Velocity Script

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 - Community Moderator

Re: Capture Specific Type from CO with Velocity Script

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

 

Jo_Pitts1
Level 10 - Community Advisor

Re: Capture Specific Type from CO with Velocity Script

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.

SanfordWhiteman
Level 10 - Community Moderator

Re: Capture Specific Type from CO with Velocity Script

Yes, it also should be $sorter.sort-ed

Jo_Pitts1
Level 10 - Community Advisor

Re: Capture Specific Type from CO with Velocity Script

@Amey_Shivapurka ,

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:

  • by iterating the list of COs and testing for both the type being registration and the date of the item being greater than the largest registration date you've found thus far.
  • by sorting the list and taking the first or last item (based on whether you sort ascending or descending - there have been posts recently on how to do this).

With either approach, you'd then display the registration outside the loop.

Cheers

Jo

 

 

 

Amey_Shivapurka
Level 2

Re: Capture Specific Type from CO with Velocity Script

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?

Jo_Pitts1
Level 10 - Community Advisor

Re: Capture Specific Type from CO with Velocity Script

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:

  1. We create a list that only contains items of type 'registration' by iterating your full list.
  2. We sort the list by date in descending order.  This puts the most recent date at the top of the array.
  3. We grab the most recent registration into the item variable (being the zeroth item in the array)
  4. Display

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

Amey_Shivapurka
Level 2

Re: Capture Specific Type from CO with Velocity Script

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>

 

Screen Shot 2021-09-29 at 11.10.15 AM.png

Again. I really appreciated your help!