Auto-populate duration based on a related record

This article explains how to create an init handler and fields change handler to update the duration of a reservation when linked to a specific record.

Prerequisites

Init Handler

Implement the following Init Handler:

global with sharing class ReservationFormInitHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { // Check if the Staff has been set B25__Reservation__c reservation = form.getReservation(); if (String.isBlank(reservation.B25__Staff__c)) { return; } // Query the staff that was selected List<B25__Staff__c> matchingStaff = [ SELECT Id, Shift_Duration__c FROM B25__Staff__c WHERE Id =: reservation.B25__Staff__c WITH SECURITY_ENFORCED ]; // Just in case the record was not present in the org. if (matchingStaff.isEmpty()) { return; } // Get the specified duration and check if it was set. Integer durationInMinutes = (Integer) matchingStaff[0].Shift_Duration__c; if (durationInMinutes == null || durationInMinutes == 0) { return; } // Calculate and update the end date based on the duration. Datetime newValue = reservation.B25__StartLocal__c.addMinutes(durationInMinutes); form.getField(B25__Reservation__c.B25__EndLocal__c).updateValue(newValue); } }

This handler first checks if the Staff field on the reservation is set. If the Staff is set it queries this object and retrieves the value of the Shift_Duration__c field. If this contains a non null non zero value it updates the local end time of the reservation to be that number of minutes after the start time.

Field Change Handler

Implement the following event handler:

global with sharing class StaffFieldUpdateHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { // Check if the Staff has been set B25__Reservation__c reservation = form.getReservation(); if (String.isBlank(reservation.B25__Staff__c)) { return; } // Query the staff that was selected List<B25__Staff__c> matchingStaff = [ SELECT Id, Shift_Duration__c FROM B25__Staff__c WHERE Id =: reservation.B25__Staff__c WITH SECURITY_ENFORCED ]; // Just in case the record was not present in the org. if (matchingStaff.isEmpty()) { return; } // Get the specified duration and check if it was set. Integer durationInMinutes = (Integer) matchingStaff[0].Shift_Duration__c; if (durationInMinutes == null || durationInMinutes == 0) { return; } // Calculate and update the end date based on the duration. Datetime newValue = reservation.B25__StartLocal__c.addMinutes(durationInMinutes); form.getField(B25__Reservation__c.B25__EndLocal__c).updateValue(newValue); } }

Adjust the Customizer Class

Now add the following line to the customize method inside your customizer class:

form.onInit(new ReservationFormInitHandler()); form.getField(B25__Reservation__c.B25__Staff__c).onUpdate(new StaffFieldUpdateHandler());

This adds both your handlers to the Reservation Contact list lookup.

Test your solution

  1. Go to the calendar.

  2. Create a new reservation on a staff record that has a duration

  3. Check if the end date was correctly updated.

  4. Select a different staff. Make sure this is a staff with a different duration.

  5. Check if the end date was correctly updated.