JavaScript Snippet Help - Find NEXT Date in Related Model

  • 1
  • Question
  • Updated 2 years ago
  • Answered
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. 
Photo of Jason Thurgood

Jason Thurgood

  • 654 Points 500 badge 2x thumb

Posted 2 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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. :D
Photo of Jason Thurgood

Jason Thurgood

  • 654 Points 500 badge 2x thumb
Great idea, but does not work if you want the column in the table to be "sortable".  
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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?
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Jason,

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.

Thanks,

Bill
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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.
Photo of Jason Thurgood

Jason Thurgood

  • 654 Points 500 badge 2x thumb
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. 
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Jason,

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.

Thanks,

Bill