Code Sample Library

This article will give you a few useful examples on how you can leverage the custom form logic feature. Other articles on this topic:

Detailed Code Samples

This section contains a few detailed code samples intended as tutorials to give you more guidance:

Quick and Dirty Code Samples

This section contains some quick examples without much explanation, intended to give you some more ideas of what is possible.

This example adds the Account name to each Contact appearing in the Contact lookup and Contact related list.

global class MyFormLogic implements B25.Form.Customizer { global void customize(B25.Form form) { form.getLookup(B25__Reservation__c.B25__Contact__c).onSearch(new ContactSearchHandler()); form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new ContactSearchHandler()); } global class ContactSearchHandler extends B25.SearchHandler { global override B25.SearchResultCollection getSearchResults(B25.SearchContext searchContext) { searchContext.setMetaTemplate('{0}', new List<String>{'Account.Name'}); return searchContext.getDefaultResults(); } } }
global with sharing class MyFormLogic implements B25.Form.Customizer { global void customize(B25.Form form) { // Trigger MyGroupHandler when the Group field changes form.getField(B25__Reservation__c.B25__Group__c).onUpdate(new MyGroupHandler()); } global with sharing class MyGroupHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { // Get all members that belong to the group Id newGroupId = (Id) event.getNewValue(); List<B25__Group_Membership__c> groupMembers = [SELECT B25__Contact__c FROM B25__Group_Membership__c WHERE B25__Group__c = :newGroupId]; // Loop through the members and add the contact to the reservation contacts for(B25__Group_Membership__c groupMember : groupMembers){ B25__ReservationContact__c reservationContact = new B25__ReservationContact__c(); reservationContact.B25__Contact_Lookup__c = groupMember.B25__Contact__c; form.getRelatedList(B25__ReservationContact__c.SObjectType).addRecord(reservationContact); } //This updates the quantity of the reservation to the amount of contacts in the group form.getField(B25__Reservation__c.B25__Quantity__c).updateValue(groupMembers.size()); } } }
global class ReservationContactAddedHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { B25__Reservation__c reservation = form.getReservation(); if (reservation.B25__Account__c == null || event.getNewValue() != 'all-contacts') { return; } 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 )); } } }