Last active
May 12, 2026 10:40
-
-
Save katherinechu92/23b58b52f87f4bee42687847956b1b38 to your computer and use it in GitHub Desktop.
Week Eight 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
| public static void handleAfterInsert(List<Account> newAccounts){ | |
| Id runningUserId = UserInfo.getUserId(); | |
| String userEmail = [SELECT Email FROM User WHERE Id = :UserInfo.getUserId()].Email; | |
| List<case> CasesToAdd = new List<case>(); | |
| for (Account a : newAccounts) { | |
| Case c = new Case(); | |
| c.Status = 'New'; | |
| c.Origin = 'New Account'; | |
| 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 = userEmail; | |
| casesToAdd.add(c); | |
| } | |
| insert casesToAdd; | |
| } |
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
| //Opportunity Trigger | |
| trigger opportunityTrigger on Opportunity (before insert) { | |
| opportunityTriggerHandler.handleAfterInsert(Trigger.new); | |
| } | |
| //Opportunity Handler | |
| 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 | |
| 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){ | |
| List<Account> updatedAccountsList = new List<Account>(); | |
| updatedAccountsList.addAll(updatedAccountsSet); | |
| update updatedAccountsList; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good job,
Line 2 - should be after insert instead of before insert
Good use of set - you could also just use the set for accountId and updatedAccountsSet to be a list instead
Minor for indentation , please use ctlA +File =>Fix indentation
Keep being amazing!