-
-
Save AngelOfCA/8861c310840c8da9fee10627fdd723db to your computer and use it in GitHub Desktop.
public with sharing class AccountTriggerHandler { | |
public static void handleBeforeInsert(List<Account> newAccounts){ | |
for (Account a : newAccounts) { | |
if (a.Est_Annual_Sales__c >= 5000000) { | |
a.Priority__c = 'Highest'; | |
} else if (a.Est_Annual_Sales__c >= 3000000) { | |
a.Priority__c = 'High'; | |
} else if (a.Est_Annual_Sales__c >= 1000000) { | |
a.Priority__c = 'Standard'; | |
} else { | |
a.Priority__c = 'Low'; | |
} | |
} | |
//this is a before trigger, so our handler doesn't have to call DML | |
} | |
public static void handleAfterInsert(Account a){ | |
User u = [SELECT Id, Email FROM User WHERE Id = :UserInfo.getUserId()]; | |
String runningUserEmail = u.Email; | |
//Week 7 Homework - bulkify this code | |
List<Case> caseList = new List<Case>(); | |
List<String> newAccounts = new List<String>; | |
for (Case c : Trigger.new) { | |
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.'; | |
caseList.add(c); | |
} | |
insert caseList; | |
} | |
public static void handleAfterUpdate(List<Account> acctsForUpdate, Map<Id, Account> oldAcctMap){ | |
//First thing we do is query for all the opportunities on accounts involved in this trigger | |
//The SOQL query below uses a nested query, this let's us pull back each acccount with a list of its opportunities attached. | |
//We won't be covering nested queries in this class, but take a look and see if you can figure out how they work | |
Map<Id, Account> acctsWithOpps = new Map<Id, Account>( | |
[SELECT Id, (SELECT Id FROM Opportunities WHERE IsClosed = false) FROM Account WHERE Id IN :acctsForUpdate] | |
); | |
//Let's make a list to hold any opportunities we create for later insertion | |
List<Opportunity> newOpportunities = new List<Opportunity>(); | |
//Now we need to loop through the accounts in this trigger and see if their priority has been changed in the way we're looking for | |
for (Account updatedAcct : acctsForUpdate) { | |
//ok, so now we have the udpated Account record, but we also need to compare it to the old version to see what has changed | |
//We can use the oldAccountMap, pass it the Account Id, and we'll get the old version for comparison | |
Account oldAcct = oldAcctMap.get(updatedAcct.Id); | |
//ok, now we have the new and old versions of the same record and we can make our comparison | |
if ((oldAcct.Priority__c != 'Highest' && oldAcct.Priority__c != 'High') && (updatedAcct.Priority__c == 'Highest' || updatedAcct.Priority__c == 'High')) { | |
//we have a winner! now check and see if the account has any Open Opportunities | |
System.debug('Number of Opportunities on this Account' + acctsWithOpps.get(updatedAcct.Id).Opportunities.size()); | |
if (acctsWithOpps.get(updatedAcct.Id).Opportunities.size() == 0) { | |
//Ok, this account has no open opportunities, let's create one | |
Opportunity opp = new Opportunity(); | |
opp.Name = updatedAcct.Name + ' Opportunity'; | |
opp.StageName = 'Prospecting'; | |
opp.CloseDate = Date.today().addMonths(3); | |
opp.AccountId = updatedAcct.Id; | |
newOpportunities.add(opp); | |
} | |
} | |
} | |
//Finally, insert any new Opportunities | |
if (newOpportunities.size() > 0) { | |
insert newOpportunities; | |
} | |
} | |
} |
Hello Erika,
It is okay to not getting it now. Let's look at the code :)
For bulkify the main thing 'never put SOQL or DML inside FOR loop if you can do it' For example, in line 33 there is an SOQL. But let's see what it does? It gets the running user email address. Technically for that method running user email id will never change so instead of Id runningUserId = UserInfo.getUserId();
doing this on line 20, you can change it to
User u = [SELECT Id, Email FROM User WHERE Id = :UserInfo.getUserId()];
String runningUserEmail = u.Email;
With this you can use runningUserEmail
in you for loop for cases.
Also on line 36 code has insert c;
but I guess this gives an error? That is because c is only defined inside for. Your approach is correct but missing a list. So correct way would be putting aList<Case> caseList = new List<Case>();
on line 22. And in a new line after line 34 caseList.add(c);
And finally insert caseList;
instead of insert c;
Last method looks good :)
If you have any questions please ask, I remember myself with bulkification when I first learned about it. It wasn't easy. :)
Hi Tugce,
Thanx for the feedback, it still isnt saving. Im getting an error with a ; ) or } on 25 &26.
Actually I just realized that for loop is wrong on the line 26. Also Line 25 missing () in the end. You need to remove line 25 all together because it is not used anywhere. Let's recap that method again.
On line 19 we have public static void handleAfterInsert(Account a){
but if we look at AccountTriggerGood we call that method like this
AccountTriggerHandler.handleAfterInsert(Trigger.new);
which is a list(Trigger.new). So line 19 should be like this public static void handleAfterInsert(List<Account> newAccounts){
After changing this line 25 needs to be deleted. And the loop should look like this
for (Account a : newAccounts) {
Case c = new Case(); //we initialize a 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.';
caseList.add(c);
}
Also next time can you tag me like this @tugce so I can get notified :)
Im still not getting how to Bulkify :/