Developer 1 - Class Basics

8 - Introduction to Apex Classes

  • Any Apex that can’t/shouldn’t go inside an Apex Trigger needs to go inside an Apex Class.
  • Specifically, Apex classes can be used to create:
    • trigger logic,
    • controllers for Visualforce pages and lightning components,
    • invokable methods for our Flows and Process Builders to call, and
    • web-service methods for external services to call.

9 - Create Basic Trigger Handler

  • Naming convention used for trigger handlers is AccountTriggerHandler and similar
    • Starting, boilerplate sytax below:
      • public: “access modifier,” specifies what other pieces of code can access this code
public class AccountTriggerHandler {
    trigger AccountTrigger on Account (before insert) {
      	AccountTriggerHandler.beforeInsertLogic(Trigger.new);
    }
}
  • Example class method (and the trigger that calls it) shown below.
    • Paradigm shown below allows us to develop more flexibly and reusably
public class AccountTriggerHandler {
    public static void beforeInsertLogic(List<Account> accountList){
        system.debug(accountList);
        accountList[0].Phone = '000-0000';
        system.debug(accountList);
    }
}
trigger AccountTrigger on Account (before insert) {
	 AccountTriggerHandler.beforeInsertLogic(Trigger.new);
}
  • After executing, debug log is as shown below. Note the METHOD_ENTRY and METHOD_EXIT events.

10 - Methods

public class AccountTriggerHandler {
    public static void beforeInsertLogic(List<Account> accountList){
        // ...
    }
}
  • You can distinguish a method from other Apex by the parentheses after the method name
  • The “body” of the method is enclosed in the brackets
  • Methods have access modifiers just like classes (“public”, etc)
  • static makes the method a “class method,” which means it can be called without creating an instance of the class
  • void is an example of a “return type”
    • Could also return a list of accounts:
      • public static List<Account> beforeInsertlogic(List<Account> accountList)
  • List<Account> accountList, enclosed in the parentheses, is a “parameter” being passed in to the method.
    • More than one can be passed:
      • public static void beforeInsertlogic(List<Account> accountList, List<Account> accountList2)

11 - Class Modifiers

//Accessible by any code in the entire org
global virtual without sharing class AccountTriggerHandler extends Exception {

    //Only accessible by code in the same namespace
    public static List<Account> beforeInsertLogic(List<Account> accountList){
        //Display new records
        System.debug(accountList);
        accountList[0].Phone = '000-0000'
        System.debug(accountList);
        return accountList;
    }

    //Test inner class
    private class innerClass {

    }

    //Only accessible by this class
    private static void debug(){
        System.debug('Hi!');
    }
}
  • Access modifiers can be applied to classes, methods, and variables. Three examples:
    • global: accessible by any code in the entire Salesforce org
    • public: accessible by code that is in the same namespace
      • “namespaces” will be familiar to people who have done development with managed packages and in Salesforce DX
      • Ex: Namespace__Custom_Object__c
    • private: accessible by the class in which it is created
      • Note the top level class cannot be set to private
      • The top level class sets the “maximum visibility” - if you want to have some methods in the class to be accessible globally, then the outermost class must be set to global
  • virtual: an example of an extra modifier that can be added in Apex so you can expand on their functionality. Another example, abstract.
    • These keywords let classes function as templates for other classes.
  • with sharing: means that the class will have sharing rules enforced.
    • without sharing means the class will not have sharing rules enforced, which means they will have more access.
    • If a class with sharing calls a class without sharing, the sharing defaults to with sharing
  • extends Exception: means classes can extend the functionality of other classes in the org, so this class would inherit some of the functionality of the Exception class.