...
Implement a form customizer class and make sure Booker25 is configured to use it, as described in the first two sections of the Quick Start Guide.
One of the objects linked to reservation has a field that contains a duration in minutes. For this example we have created a new field on B25__Staff__c. (B25__Shift_Duration__c)
Init Handler
...
Code Block | ||
---|---|---|
| ||
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, B25__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].B25__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 B25__ 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.
...
Code Block |
---|
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, B25__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].B25__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:
...