Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. public with sharing class AccountTriggerHandler {
  2.  
  3. public static void handleBeforeInsert(List<Account> newAccounts){
  4. for (Account a : newAccounts) {
  5. if (a.Est_Annual_Sales__c >= 5000000) {
  6. a.Priority__c = 'Highest';
  7. } else if (a.Est_Annual_Sales__c >= 3000000) {
  8. a.Priority__c = 'High';
  9. } else if (a.Est_Annual_Sales__c >= 1000000) {
  10. a.Priority__c = 'Standard';
  11. } else {
  12. a.Priority__c = 'Low';
  13. }
  14. }
  15. //this is a before trigger, so our handler doesn't have to call DML
  16. }
  17.  
  18. public static void handleAfterInsert(List<Account> newAccounts){
  19.  
  20. Id runningUserId = UserInfo.getUserId();
  21. List<Case> myList=New list <Case>();
  22. //Week 7 Homework - bulkify this code
  23. for (Account a : newAccounts) {
  24. Case c = new Case();
  25. c.Status = 'New';
  26. c.Origin = 'New Account'; //Make sure you've added this as a picklist value for this field
  27. c.Subject = 'Send Welcome Package';
  28. c.AccountId = a.Id;
  29. c.Description = 'Please follow up with this new Account and send them a Welcome Package.';
  30. myList.add(c);
  31.  
  32. //Get the email address for the Account owner
  33. User u = [SELECT Id, Email FROM User WHERE Id = :runningUserId];
  34. c.Staff_Email_Address__c = u.Email;
  35. System.debug('New Case: ' + myList.size());
  36. insert c;
  37. }
  38. }
  39.  
  40. public static void handleAfterUpdate(List<Account> acctsForUpdate, Map<Id, Account> oldAcctMap){
  41. //First thing we do is query for all the opportunities on accounts involved in this trigger
  42. //The SOQL query below uses a nested query, this let's us pull back each acccount with a list of its opportunities attached.
  43. //We won't be covering nested queries in this class, but take a look and see if you can figure out how they work
  44. Map<Id, Account> acctsWithOpps = new Map<Id, Account>(
  45. [SELECT Id, (SELECT Id FROM Opportunities WHERE IsClosed = false) FROM Account WHERE Id IN :acctsForUpdate]
  46. );
  47.  
  48. //Let's make a list to hold any opportunities we create for later insertion
  49. List<Opportunity> newOpportunities = new List<Opportunity>();
  50.  
  51. //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
  52. for (Account updatedAcct : acctsForUpdate) {
  53.  
  54.  
  55. //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
  56. //We can use the oldAccountMap, pass it the Account Id, and we'll get the old version for comparison
  57. Account oldAcct = oldAcctMap.get(updatedAcct.Id);
  58.  
  59. //ok, now we have the new and old versions of the same record and we can make our comparison
  60. if ((oldAcct.Priority__c != 'Highest' && oldAcct.Priority__c != 'High') && (updatedAcct.Priority__c == 'Highest' || updatedAcct.Priority__c == 'High')) {
  61. //we have a winner! now check and see if the account has any Open Opportunities
  62.  
  63. System.debug('Number of Opportunities on this Account' + acctsWithOpps.get(updatedAcct.Id).Opportunities.size());
  64.  
  65. if (acctsWithOpps.get(updatedAcct.Id).Opportunities.size() == 0) {
  66. //Ok, this account has no open opportunities, let's create one
  67. Opportunity opp = new Opportunity();
  68. opp.Name = updatedAcct.Name + ' Opportunity';
  69. opp.StageName = 'Prospecting';
  70. opp.CloseDate = Date.today().addMonths(3);
  71. opp.AccountId = updatedAcct.Id;
  72. newOpportunities.add(opp);
  73. }
  74. }
  75. }
  76.  
  77. //Finally, insert any new Opportunities
  78. if (newOpportunities.size() > 0) {
  79. insert newOpportunities;
  80. }
  81.  
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement