Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 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. //Week 7 Homework - bulkify this code
  22. //for (Account a : newAccounts) {
  23. //Case c = new Case();
  24. //c.Status = 'New';
  25. //c.Origin = 'New Account'; //Make sure you've added this as a picklist value for this field
  26. //c.Subject = 'Send Welcome Package';
  27. //c.AccountId = a.Id;
  28. //c.Description = 'Please follow up with this new Account and send them a Welcome Package.';
  29.  
  30. //Get the email address for the Account owner
  31. //User u = [SELECT Id, Email FROM User WHERE Id = :runningUserId];
  32. //c.Staff_Email_Address__c = u.Email;
  33. //insert c;
  34.  
  35. //get the running ID of the user, so we can add it to the case
  36. Id runningUserId = UserInfo.getUserId();
  37. //get the email for the running user to add to the case
  38. User u = [SELECT Id, Email FROM User WHERE Id = :runningUserID];
  39. //Week 7 Homework - bulkify this code
  40.  
  41. //create a list of cases to insert in bulk
  42. List<Case> caseList = new List <Case>();
  43.  
  44. //loop through new accounts and create cases, then add them to the CaseList
  45. for (Account a : newAccounts) {
  46. //create the case and set the fields
  47. Case c = new Case();
  48. c.Status = 'New';
  49. c.Origin = 'New Account'; //Make sure you've added this as a picklist value for this field
  50. c.Subject = 'Send Welcome Package';
  51. c.AccountId = a.Id;
  52. c.Description = 'Please follow up with this new Account and send them a Welcome Package.';
  53. c.Staff_Email_Address__c = u.Email;
  54. //add the case to the CaseList
  55. caseList.add(c);
  56. }
  57. //check if there are cases and if yes, insert the entire list
  58. if (caseList.size()>0){
  59. insert CaseList;
  60. }
  61. }
  62.  
  63. public static void handleAfterUpdate(List<Account> acctsForUpdate, Map<Id, Account> oldAcctMap){
  64. //First thing we do is query for all the opportunities on accounts involved in this trigger
  65. //The SOQL query below uses a nested query, this lets us pull back each acccount with a list of its opportunities attached.
  66. //We won't be covering nested queries in this class, but take a look and see if you can figure out how they work
  67. Map<Id, Account> acctsWithOpps = new Map<Id, Account>(
  68. [SELECT Id, (SELECT Id FROM Opportunities WHERE IsClosed = false) FROM Account WHERE Id IN :acctsForUpdate]
  69. );
  70.  
  71. //Let's make a list to hold any opportunities we create for later insertion
  72. List<Opportunity> newOpportunities = new List<Opportunity>();
  73.  
  74. //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
  75. for (Account updatedAcct : acctsForUpdate) {
  76.  
  77. //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
  78. //We can use the oldAccountMap, pass it the Account Id, and we'll get the old version for comparison
  79. Account oldAcct = oldAcctMap.get(updatedAcct.Id);
  80.  
  81. //ok, now we have the new and old versions of the same record and we can make our comparison
  82. if ((oldAcct.Priority__c != 'Highest' && oldAcct.Priority__c != 'High') && (updatedAcct.Priority__c == 'Highest' || updatedAcct.Priority__c == 'High')) {
  83. //we have a winner! now check and see if the account has any Open Opportunities
  84.  
  85. System.debug('Number of Opportunities on this Account' + acctsWithOpps.get(updatedAcct.Id).Opportunities.size());
  86.  
  87. if (acctsWithOpps.get(updatedAcct.Id).Opportunities.size() == 0) {
  88. //Ok, this account has no open opportunities, let's create one
  89. Opportunity opp = new Opportunity();
  90. opp.Name = updatedAcct.Name + ' Opportunity';
  91. opp.StageName = 'Prospecting';
  92. opp.CloseDate = Date.today().addMonths(3);
  93. opp.AccountId = updatedAcct.Id;
  94. newOpportunities.add(opp);
  95. }
  96. }
  97. }
  98.  
  99. //Finally, insert any new Opportunities
  100. if (newOpportunities.size() > 0) {
  101. insert newOpportunities;
  102. }
  103.  
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement