Skip to content

Instantly share code, notes, and snippets.

trigger LeadTrigger on Lead (before insert, after insert, before update, after update,
before delete, after delete, after undelete) {
new LeadTriggerHandler().run();
}
public class LeadTriggerHandler extends TriggerHandler {
List<Lead> newLeads;
public LeadTriggerHandler(){
newLeads = (List<Lead>) Trigger.new;
}
public override void beforeInsert() {
Set<String> companyNames = new Set<String>(); // Will hold the set of Lead Company Names
public virtual class TriggerHandler {
// Current context of the trigger, overridable in tests
@TestVisible
private TriggerContext context;
// Constructor
public TriggerHandler() {
this.setTriggerContext();
}
trigger LeadTrigger on Lead (before insert, after insert, before update, after update,
before delete, after delete, after undelete) {
if(Trigger.isBefore && Trigger.isInsert) {
LeadTriggerHandler.handleBeforeInsert(Trigger.new);
}
if(Trigger.isAfter && Trigger.isInsert) {
LeadTriggerHandler.handleAfterInsert(Trigger.new);
}
public class LeadTriggerHandler {
public static void handleBeforeInsert(List<Lead> newLeads) {
Set<String> companyNames = new Set<String>(); // Will hold the set of Lead Company Names
Map<String, ID> accountNamesToId = new Map<String, ID>(); // Will hold the set of Account Names mapped to the Account ID
List<Case> newCases = new List<Case>(); // Will hold the new cases to create
for(Lead l : newLeads){ // Look at each Lead individually
companyNames.add(l.Company); // Add the Lead Company to our set
}
// Trigger to search Accounts and create a new Case if a Lead Company Name is being created that matches an existing Account name
trigger LeadTrigger on Lead (before insert) {
Set<String> companyNames = new Set<String>(); // Will hold the set of Lead Company Names
Map<String, ID> accountNamesToId = new Map<String, ID>(); // Will hold the set of Account Names mapped to the Account ID
List<Case> newCases = new List<Case>(); // Will hold the new cases to create
for(Lead l : Trigger.new){ // Look at each Lead individually
companyNames.add(l.Company); // Add the Lead Company to our set
}