Show only Contacts linked to the Account

This page demonstrates two ways in which you can filter Contacts:

  1. Filter the Contact lookup.

  2. Filter the Reservation Contacts related list.

Of course, the same principles apply to any other lookups or related lists, and you can implement this solution for your own custom lookups or releated lists.

Filter the Contact lookup

Prerequisites

Handler

Implement the following search handler:

1 2 3 4 5 6 7 8 9 global with sharing class ContactSearchHandler extends B25.SearchHandler { global override B25.SearchResultCollection getSearchResults(B25.SearchContext context) { Id accountId = context.getForm().getReservation().B25__Account__c; if (accountId != null) { context.addCondition('AccountId = \'' + accountId + '\''); } return context.getDefaultResults(); } }

This handler first checks if the Account field on the reservation is set. If the Account is set, it adds a condition to the search context to filter the results based on the Account id. It then returns the results.

Customizer

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

1 form.getLookup(B25__Reservation__c.B25__Contact__c).onSearch(new ContactSearchHandler());

This adds your handler to the Contact lookup.

Test your solution

  1. Go to the calendar.

  2. Create a new reservation.

  3. Select an account.

  4. Select a contact in the lookup. Notice how only contacts for the selected account are shown.

Prerequisites

Handler

The search handler for this example is the same as in the previous section (Filter the Contact lookup). Refer to that section if you have not yet created it.

Customizer

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

1 form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new ContactSearchHandler());

This adds your handler to the Reservation Contacts related list.

Test your solution

  1. Go to the calendar.

  2. Create a new reservation.

  3. Select an account.

  4. Select a contact in the related list. Notice how only contacts for the selected account are shown.

Example of the final result