Skip to main content
Skip table of contents

Add all Contacts belonging to the Account

This article explains how to add a special option in the Contacts list lookup. When selected, this option will add all Contacts belonging to the Account.

Prerequisites

Search Handler

Implement the following search handler:

CODE
global with sharing class ContactSearchHandler extends B25.SearchHandler {
    global override B25.SearchResultCollection getSearchResults(B25.SearchContext context) {
        B25.SearchResultCollection resultCollection = new B25.SearchResultCollection();
        if (context.getForm().getReservation().B25__Account__c != null) {
            // Add a special search result
            resultCollection.addSearchResult(
                new B25.SearchResult('all-contacts', 'Add all Contacts of the current Account')
                    .setPreventDefault(true) // this line prevents GoMeddo from trying to add a record to the list
                    .setIcon('standard:contact_list')
            );
        }
        // Now also include the default GoMeddo search results
        resultCollection.addSearchResults(context.getDefaultResults().getSearchResults());
        return resultCollection;
    }
}

This handler first checks if the Account field on the reservation is set. If the Account is set, it adds a special option in the results that the user can select. Note the special option has an id of ‘all-contacts’. We will use this id later in the other handler to detect if the special option was selected.

Event Handler

Implement the following event handler:

JAVA
global with sharing class ReservationContactHandler extends B25.FormEventHandler {
    global override void handleEvent(B25.FormEvent event, B25.Form form) {
        // Check if the Account has been set and selected option was the special option 'all-contacts'
        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_Lookup__c = contact.Id
            ));
        }
    }
}

Customizer

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

JAVA
form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new ContactSearchHandler());
form.getRelatedList(B25__ReservationContact__c.SObjectType).onAdd(new ReservationContactHandler());

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

Test your solution

  1. Go to the calendar.

  2. Create a new reservation.

  3. Select an account. Make sure this is an account with multiple contacts.

  4. Place your cursor in the Contacts list lookup.

  5. Select the special option 'Add all Contacts of the current Account'.

  6. Notice that all contacts have been added.

Example of the final result

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.