Velocitip: Find the interesting object in Trigger and Batch modes

It's fairly common (in my world) for a Velocity script to need to refer to a triggering object (if someone has a new or updated Custom Object or Opportunity) or, in a batch context, to find the most recently updated object of the same type.

That is, go straight to $TriggerObject if you can, but if it's not there, you need to consult the list of objects to get what you want.

You can switch automatically like so:

#if( $context.contains("TriggerObject") )
#set( $focusedObject = $TriggerObject )
#elseif( $context.contains("CustomObject_cList") )
#set( $focusedObject = $sorter.sort($CustomObject_cList,"updatedAt:desc")[0] )
#end

Of course, one might wonder why you wouldn't just go to the list all the time, especially now that the 10-record limitation has been lifted. Yeah, you can. :)