Created
April 15, 2018 23:27
-
-
Save adam17amo/0cd452e7cd361cf3fcc65f21c96a11d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| // Get all existing Accounts with the same names as the Leads being inserted | |
| List<Account> existingAccounts = [SELECT Name FROM Account WHERE Name IN :companyNames]; | |
| for(Account a : existingAccounts) { // Look at each Account individually | |
| accountNamesToId.put(a.Name, a.Id); // Add all Account names to a set and map it to the Account ID | |
| } | |
| for(Lead l : newLeads) { // Go through list of Leads again | |
| if(accountNamesToId.keySet().contains(l.Company)) { // If the Lead Company name is in the list of existing Account names | |
| Case c = new Case(); // Create a new case | |
| c.AccountId = accountNamesToId.get(l.Company); // Get the Account ID that corresponds to this Account Name | |
| c.Subject = 'Account already exists for ' + l.Company; // Set the subject | |
| c.Status = 'New'; // Set the status | |
| newCases.add(c); // Add our Case to the list of Cases to create | |
| } | |
| } | |
| insert newCases; // Insert our Cases into Salesforce | |
| } | |
| public static void handleAfterInsert(List<Lead> newLeads) { | |
| } | |
| public static void handleBeforeUpdate(List<Lead> newLeads, List<Lead> oldLeads, | |
| Map<ID, Lead> newMap, Map<ID, Lead> oldMap) { | |
| } | |
| public static void handleAfterUpdate(List<Lead> newLeads, List<Lead> oldLeads, | |
| Map<ID, Lead> newMap, Map<ID, Lead> oldMap) { | |
| } | |
| public static void handleBeforeDelete(List<Lead> oldLeads) { | |
| } | |
| public static void handleAfterDelete(List<Lead> oldLeads) { | |
| } | |
| public static void handleAfterUndelete(List<Lead> newLeads) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment