Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleImmediately add all group members as reservation contacts when selecting a group.
Code Block
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());
        }
    }
}

...