global with sharing class MyFormLogic implements B25.Form.Customizer {
global void customize(B25.Form form) {
// When searching the ReservationContact related list, call a handler that adds an extra option
form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new MySearchHandler());
// When a ReservationContact is added, call a handler that checks if the extra option was chosen
form.getRelatedList(B25__ReservationContact__c.SObjectType).onAdd(new MyReservationContactHandler());
}
global class MySearchHandler implementsextends B25.SearchContext.SearchHandler {
public override B25.SearchResult.CollectionSearchResultCollection getSearchResults(B25.SearchContext context) {
B25.SearchResult.CollectionSearchResultCollection resultCollection = new B25.SearchResult.CollectionSearchResultCollection();
// Here we add a new entry to the search results with the value all-contacts and label All Contacts
// We also set Prevent Default to true, which prevents Booker25 from adding the item to the list when clicked
if (context.getForm().getReservation().B25__Account__c != null) {
resultCollection.addSearchResult(
new B25.SearchResult('all-contacts', 'Add all Contacts linked to current Account')
.setPreventDefault(true)
.setIcon('standard:contact_list')
);
}
// Whe then add the default Booker25 search results to the search results.
resultCollection.addSearchResults(context.getDefaultResults().getSearchResults());
return resultCollection;
}
}
global with sharing class MyReservationContactHandler extends B25.FormEventHandler {
global override void handleEvent(B25.FormEvent event, B25.Form form) {
// First we detect if the Account has been set and the value clicked was the special option we added in MySearchHandler
B25__Reservation__c reservation = form.getReservation();
if (reservation.B25__Account__c == null || event.getNewValue() != 'all-contacts') {
return;
}
// Now add all Contacts linked to the Account
for (Contact contact : [SELECT Id FROM Contact WHERE AccountId = :reservation.B25__Account__c]) {
form.getRelatedList(B25__ReservationContact__c.SObjectType).addRecord(new B25__ReservationContact__c(
B25__Contact__c = contact.Id
));
}
}
}
} |