JavaScript Snippet Help - Find NEXT Date in Related Model

Apologies, I still don’t know much JavaScript, but I am sure this is something that can be addressed fairly easily. Any guidance is much appreciated.

I have a model for Case and a model for FutureAppointments, I am currently using a MODEL_LOOKUP formula to pull the Next Appointment up to the Case Table, BUT it is not a sortable column, which is what I would like to accomplish.

Because Next Appointment will always be a moving target, I am looking to create a JavaScript Snippet that can be invoked whenever a model is queried (page load, and subsequent queries - maybe through a Refresh button on the Page) - any time the snippet is invoked the snippet would loop through all Appointment__c records and find the first record in the FutureAppointments model related to the Case ID.

Assume there will be more than 1 case listed on this page, the Appointment Table is just there for reference, but would be removed. Just after a list of Cases, and the ability to sort by Next Appointment.

Hmmmm… another solution would be to add a Child Relationship to the Event object from the Case object. Just set the limit of records in the child relationship to 1 and sort by the date field in DESC. No snippet required. :smiley:


Instead of a JavaScript snippet, this seems like a good solution for a trigger to update the Case with the date of the next appointment.  Then you could sort by the date without a snippet and show this field on any page.



Oh yeah! Server side. Then how about Declarative Lookup Summary Tool to set the date. Set up the sort in the same way as the child relationship.

Great idea, but does not work if you want the column in the table to be “sortable”.  

Triggers are expensive in this org.  In a call center environment adding triggers may be more costly in terms of performance and scalability.  I will keep thinking on this. 


Does the update have to be real time?  You could setup a batch class that would do the update.  It has much higher limits than triggers.

This is a ‘tricky’ requirement when you want to make the column sortable.



Then server side solution would be better as the value would reside on the parent record.

Is the child record Master-Detail? Then standard Rollups.

If not, can you make it Master-Detail?