Versions Compared

Key

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

...

  1. Create a new global Apex class. This can also be an inner class inside the MyFormLogic class that we created earlier. For simplicity, this is what we will do in all our code samples. In this example we will name our inner class MyStatusHandler.

  2. Have the new class extend the B25.FormEventHandler class. This means you will have to add the ‘handleEvent’ method.

  3. Inside the handleEvent method, add the following code that sets the title based on the status:

    Code Block
    Id newStatusId = (Id) event.getNewValue();
    B25__Reservation_Status__c status = [
        SELECT Name
        FROM B25__Reservation_Status__c
        WHERE Id = :newStatusId
    ];
    form.getFieldgetActiveRecord().put(B25__Reservation__c.B25__Title__c).updateValue(, status.Name);
  4. Inside the customize method of the MyFormLogic class, add the following line to make the form listen to changes in the status:

    Code Block
    form.getField(B25__Reservation__c.B25__Status__c).onUpdate(new MyStatusHandler());
  5. Save MyFormLogic. The entire file should now look something like this:

    Code Block
    global with sharing class MyFormLogic implements B25.Form.Customizer {
    
        global void customize(B25.Form form) {
            // this is where we will add our handlers to the form
            form.getField(B25__Reservation__c.B25__Status__c).onUpdate(new MyStatusHandler());
        }
        
        global with sharing class MyStatusHandler extends B25.FormEventHandler {
            global override void handleEvent(B25.FormEvent event, B25.Form form) {
                Id newStatusId = (Id) event.getNewValue();
                B25__Reservation_Status__c status = [
                    SELECT Name
                    FROM B25__Reservation_Status__c
                    WHERE Id = :newStatusId
                ];
                form.getFieldgetActiveRecord().put(B25__Reservation__c.B25__Title__c).updateValue(, status.Name);
            }
        }
    }
  6. Test out your functionality by creating a new reservation on the calendar, and changing the status. You should be able to see the title change as well.

...

  1. Add another inner class to the MyFormLogic class, but this time extending the B25.SearchHandler class. We will name our example class MyContactSearch.

  2. Override the ‘getSearchResults’ method defined in the B25.SearchHandler class. Inside the method, add the following logic:

    Code Block
    Id accountId = context.getForm().getReservation().B25__Account__c;
    if (accountId != null) {
        context.addCondition('AccountId = \'' + accountId + '\'');
    }
    return context.getDefaultResults();
  3. Inside the customize method of the MyFormLogic class, add the following line to make the form listen to searches in the reservation contact list:

    Code Block
    form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new MyContactSearch());
  4. Save MyFormLogic. The entire file should now look something like this:

    Code Block
    global with sharing class MyFormLogic implements B25.Form.Customizer {
    
          global void customize(B25.Form form) {
            // this is where we will add our handlers to the form
            form.getField(B25__Reservation__c.B25__Status__c).onUpdate(new MyStatusHandler());
            form.getRelatedList(B25__ReservationContact__c.SObjectType).onSearch(new MyContactSearch());
        }
        
        global with sharing class MyStatusHandler extends B25.FormEventHandler {
            global override void handleEvent(B25.FormEvent event, B25.Form form) {
                Id newStatusId = (Id) event.getNewValue();
                B25__Reservation_Status__c status = [
                    SELECT Name
                    FROM B25__Reservation_Status__c
                    WHERE Id = :newStatusId
                ];
                form.getActiveRecord().getFieldput(B25__Reservation__c.B25__Title__c).updateValue(, status.Name);
            }
        }
        
        global with sharing class MyContactSearch 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();
            }
        }
    }
  5. Test out your functionality by creating a new reservation on the calendar, and selecting an account. Now search for contacts to add, and you should only see results for the selected account.