Created
December 14, 2025 00:44
-
-
Save JessWingerden/6866ff76a5995ee83a6e7618311a39e1 to your computer and use it in GitHub Desktop.
Week 8 Homework
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
| AccountTriggerHandler.handleAfterInsert | |
| public static void handleAfterInsert(List<Account> newAccounts){ | |
| Id runningUserId = UserInfo.getUserId(); | |
| //JW: Only need to do this query once outside of the loop. | |
| User u = [SELECT Id, Email FROM User WHERE Id = :runningUserId]; | |
| //JW:Create an empty case list to store the case information | |
| List<Case> casesToInsert = new List<Case>(); | |
| //Homework - bulkify this code | |
| for (Account a : newAccounts) { | |
| Case c = new Case(); | |
| c.Status = 'New'; | |
| c.Origin = 'New Account'; //Make sure you've added this as a picklist value for this field | |
| c.Subject = 'Send Welcome Package'; | |
| c.AccountId = a.Id; | |
| c.Description = 'Please follow up with this new Account and send them a Welcome Package.'; | |
| c.Staff_Email_Address__c = u.Email; | |
| //JW:Update this so each time it loops it adds the record into the list collection variable | |
| casesToInsert.add(c); | |
| } | |
| //JW: The insert action is always outside the loop so its only 1 action | |
| insert casesToInsert; | |
| } | |
| OpportunityTrigger | |
| trigger OpportunityTrigger on Opportunity (after insert) { | |
| OpportunityTriggerHandler.handleAfterInsert(Trigger.new); | |
| } | |
| OpportunityTriggerHandler | |
| public class OpportunityTriggerHandler { | |
| public static void handleAfterInsert(List<Opportunity> newOpps){ | |
| //When an Opportunity is inserted, check and see if this account has a rating of 'Hot'. If not, make it 'Hot' | |
| //since we consider any account with an open opportunity as hot | |
| //Get a map of account records that have opportunities in this trigger | |
| //In order to do this, we first need a set of the Account Ids// 1) Collect Account Ids from the inserted Opportunities | |
| Set<Id> accountIds = new Set<Id>(); | |
| for (Opportunity opp : newOpps) { | |
| accountIds.add(opp.AccountId); | |
| } | |
| //Now we can get the map | |
| Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id, Rating FROM Account WHERE Id IN :accountIds]); | |
| //Finally, a set to hold any accounts we're updating (we use a set, because it's possible to have multiple opportunities being | |
| //inserted for an account and they would then get added to the list twice. We'll convert this set back to a list before doing DML ) | |
| Set<Account> updatedAccountsSet = new Set<Account>(); | |
| //ok, now we loop through and update as needed | |
| for (Opportunity opp : NewOpps) { | |
| //get the account record for this opportunity | |
| Account acct = accountMap.get(opp.AccountId); | |
| if (acct.Rating != 'Hot') { | |
| acct.Rating = 'Hot'; | |
| updatedAccountsSet.add(acct); | |
| } | |
| } | |
| if (updatedAccountsSet.size() > 0) { | |
| //convert the set to a list for DML | |
| List<Account> updatedAccounts = new List<Account>(); | |
| //Add all the accounts from the set into our list | |
| updatedAccounts.addAll(updatedAccountsSet); | |
| update updatedAccounts; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment