- Created by Maarten Lapere, last modified by Stijn de Vries on Aug 19, 2020
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 22 Next »
This article will give you a few useful examples on how you can leverage the custom form logic feature. Other articles on this topic:
Custom Form Logic for a high level overview
Quick Start Guide to help you get your first implementation up and running
API Reference for details on the methods and objects you can use
Code Samples
This section currently contains some quick examples without much explanation. For a more thoroughly detailed example, seeShow only Contacts linked to the Account
global with sharing class MyFormLogic implements B25.FormAPI.Customizable { 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 implements B25.FormAPI.EventHandler { global 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__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 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 extends B25.SearchHandler { public override B25.SearchResultCollection getSearchResults(B25.SearchContext context) { B25.SearchResultCollection resultCollection = new B25.SearchResultCollection(); // 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 )); } } } }
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__c = contact.Id )); } } }
global class ReservationContactSearchHandler implements B25.SearchContext.Handler { global B25.SearchResult.Collection getSearchResults(B25.SearchContext searchContext) { B25.SearchResult.Collection searchCollection = searchContext.getDefaultResults(); B25.SearchResult.Collection updatedCollection = new B25.SearchResult.Collection(); if (searchContext.form.getReservation().B25__Account__c != null) { updatedCollection.addSearchResult( new B25.SearchResult('all-contacts', 'Add all contacts linked to current account') .setPreventDefault(true) .setIcon('standard:contact_list') ); } updatedCollection.addSearchResults(searchCollection.getSearchResults()); return updatedCollection; } }
global class ReservationTitleUpdateHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { String newTitleValue = (String) event.getNewValue(); List<B25__Reservation_Status__c> matchingStatus = [SELECT Id FROM B25__Reservation_Status__c WHERE Name = :newTitleValue]; if (!matchingStatus.isEmpty()) { form.getField(B25__Reservation__c.B25__Status__c).updateValue(matchingStatus[0].Id); } } }
global class ReservationAccountChangeHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { String newAccountValue = (Id) event.getNewValue(); if (String.isBlank(newAccountValue)) { for(RelatedListItem item : form.getRelatedList(B25__ReservationContact__c.SObjectType).getItems()) { item.remove(); } } } }
global class ReservationStatusChangeHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { Id newStatus = form.getReservation().B25__Status__c; List<B25__Reservation_Status__c> reservationStatus = [SELECT Id, Name FROM B25__Reservation_Status__c WHERE id = :newStatus]; if (reservationStatus.isEmpty()) { return; } if (reservationStatus[0].Name == 'Completed') { for(B25.RelatedListItem item : form.getRelatedList(B25__ReservationContact__c.SObjectType).getItems()) { item.getField(B25__ReservationContact__c.B25__CheckedIn__c).updateValue(true); } } else { for(B25.RelatedListItem item : form.getRelatedList(B25__ReservationContact__c.SObjectType).getItems()) { item.getField(B25__ReservationContact__c.B25__CheckedIn__c).updateValue(false); } } } }
global class ReservationContactRemoveHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { B25__ReservationContact__c removedReservationContact = (B25__ReservationContact__c) form.getRelatedList(B25__ReservationContact__c.SObjectType).getItemByGuid(event.guid).getRecord(); if (form.getReservation().B25__Contact__c == removedReservationContact.B25__Contact__c) { form.getRelatedList(B25__ReservationContact__c.SObjectType).addRecord(removedReservationContact); } } }
global class ResourceLookupSearchHandler extends B25.SearchHandler { global override B25.SearchResultCollection getSearchResults(B25.SearchContext searchContext) { B25.SearchResultCollection searchCollection = searchContext.getDefaultResults(); for (B25.SearchResult result : searchCollection.getSearchResults()) { result.setIcon(this.getRandomIconName()); } return searchCollection; } private String getRandomIconName() { return 'custom:custom' + String.valueOf(Math.Round(Math.Random() * (113-1) + 1)); } }
global class FormInitHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { if (form.getReservation().Id == null) { form.getField(B25__Reservation__c.Title__c).updateValue('New Reservation'); } } }
global class FormEditHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { if (form.getReservation().Id == null) { return; } if (!String.isBlank(form.getReservation().B25__Title__c) && !form.getReservation().B25__Title__c.endsWith('(Edited)')) { form.getField(Reservation__c.B25__Title__c).updateValue(form.getReservation().B25__Title__c + ' (Edited)'); } else { form.getField(Reservation__c.B25__Title__c).updateValue('(Edited)'); } } }
global class ReservationContactNotesChangeHandler extends B25.FormEventHandler { global override void handleEvent(B25.FormEvent event, B25.Form form) { String newNotesValue = (String) event.getNewValue(); if (!String.isBlank(newNotesValue)) { if (newNotesValue.containsIgnoreCase('Checked In')) { form.getRelatedList(B25__ReservationContact__c.SObjectType).getitembyGuid(event.guid).getField(B25__ReservationContact__c.B25__CheckedIn__c).updateValue(true); } else if (newNotesValue.containsIgnoreCase('Checked Out')) { form.getRelatedList(B25__ReservationContact__c.SObjectType).getitembyGuid(event.guid).getField(B25__ReservationContact__c.B25__CheckedIn__c).updateValue(false); } } } }
global class AccountLookupSearchHandler extends B25.SearchHandler { global override B25.SearchResultCollection getSearchResults(B25.SearchContext searchContext) { B25.SearchResultCollection collection = new B25.SearchResultCollection(); for (Account account : [SELECT Id, Name, (SELECT Name FROM Contacts LIMIT 2) FROM Account LIMIT 20]) { List<String> metaTextArray = new List<String>(); for (Contact contact : account.Contacts) { metaTextArray.add(contact.Name); } collection.addSearchResult(new B25.SearchResult(account.Id, account.Name).setMetaText(String.join(metaTextArray, ', '))); } return collection; } }
- No labels