SOLVED

Yay, another Velocity question

Go to solution
Anonymous
Not applicable

Yay, another Velocity question

A while back, I had an issue with getting our script to pull up the most recent entry of a custom object, and the community (or, well, Sanford Whiteman​) came to the rescue and helped me fix it.

Seems my fix was almost a little too good, because now it's pulling the most recent entry, even in cases where that entry is invalid. In this case, we're using custom objects to pull data for appointment confirmation emails. If a customer decides, "oh hey, I want to book a new appointment," they'll book a new one and cancel the old one. If they cancel the old one after booking the new one, that's now the object entry with the most recent modifications, so they're getting the cancelled data instead of the actual appointment.

Here's the script for what I'm using currently:

#if( $appointment_cList && !$appointment_cList.isEmpty() ) 

#set( $firstItem = $appointment_cList[0] ) 

#set( $lastItem = $appointment_cList[$math.sub($appointment_cList.size(),1)] ) 

#end

$lastItem.appointmentDate

Simple, and works great. What I'm wondering is, we have a field in there called 'Order Status,' and in that field it will show "SCHEDULED" "CANCELED" etc. How can I modify the script to where it suppresses against field entries of 'canceled' for that particular field?

The direction I'm looking in is:

#if( $orderStatus == 'CANCELED')

#set( $lastItem.appointmentDate [this is the part where I need help with syntax to tell it to go down one])

Or would it make more sense to do it as:

#if( $orderStatus == 'CANCELED')

#if( $orderStatus == 'SCHEDULED')

#set( $lastItem.appointmentDate [inter syntax here to get it to display the object w/ the scheduled value...again not sure exactly what that would look like!])

Haalllllpppp....

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Yay, another Velocity question

Hey Alex, sorry for the delay. Usually get to these quickly!

The answer is to filter the list by only those that have "SCHEDULED." Velocity forces us to be clunky (there's no shortcut like ArrayList.filter() because you can't use predicates in VTL... but I digress) but it works:

#set( $appointment_cList_scheduled = [] )

#foreach( $appt in $appointment_cList )

#if( $appt.orderStatus == "SCHEDULED" )

#set( $tmp = $appointment_cList_scheduled.add($appt) )

#end

#end

#if( $appointment_cList_scheduled && !$appointment_cList_scheduled.isEmpty() )

#set( $firstItem = $appointment_cList_scheduled[0] )

#set( $lastItem = $appointment_cList_scheduled[$math.sub($appointment_cList_scheduled.size(),1)] )

#end

$lastItem.appointmentDate

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Yay, another Velocity question

Hey Alex, sorry for the delay. Usually get to these quickly!

The answer is to filter the list by only those that have "SCHEDULED." Velocity forces us to be clunky (there's no shortcut like ArrayList.filter() because you can't use predicates in VTL... but I digress) but it works:

#set( $appointment_cList_scheduled = [] )

#foreach( $appt in $appointment_cList )

#if( $appt.orderStatus == "SCHEDULED" )

#set( $tmp = $appointment_cList_scheduled.add($appt) )

#end

#end

#if( $appointment_cList_scheduled && !$appointment_cList_scheduled.isEmpty() )

#set( $firstItem = $appointment_cList_scheduled[0] )

#set( $lastItem = $appointment_cList_scheduled[$math.sub($appointment_cList_scheduled.size(),1)] )

#end

$lastItem.appointmentDate

Anonymous
Not applicable

Re: Yay, another Velocity question

Flawless! You, kind sir, are a gentleman and a scholar. Thank you!!!