Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. global class ImmediateAppointmentConfirmation implements Schedulable {
  2. global void execute(SchedulableContext ctx) {
  3. if (DateTime.now().hour() >= 8 && DateTime.now().hour() <= 20 || Test.isRunningTest()) {
  4. Boolean errorFlag = false;
  5. String Email;
  6. String Name = '';
  7. String contentDocumentID;
  8. String SalesRepPhotoURL;
  9. String SalesRepName;
  10. String SalesRepEmail;
  11. String Company = 'Peerless Fence';
  12. String OrgWideEmailAddress = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: 'Peerless Fence Group'].Id;
  13. String HDOrgEmailAddress = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: 'Home Depot Fence Service'].Id;
  14. String LogoURL = '"https://peerlessfence.com/sites/default/files/logo-01.png"';
  15. Set <Id> WhoIDs = new Set <Id>();
  16. Set <Id> WhatIDs = new Set <Id>();
  17. Set <Id> UserIds = new Set <Id>();
  18. Map<Id, Event> EventDetails = new Map<Id, Event> ([SELECT Id, OwnerID, CreatedDate, WhoId, WhatId, StartDateTime, Confirmation_Sent__c, Type__c, Send_Confirmation__c FROM Event WHERE CreatedDate >= YESTERDAY AND Confirmation_Sent__c = FALSE AND Send_Confirmation__c = True AND Type__c = 'Sales Appointment' AND WhoId != NULL AND WhatId != NULL LIMIT 50]);
  19. Map<String, Opportunity> OpportunityMap = new Map<String, Opportunity>();
  20. Map<String, Contact> ContactMap = new Map<String, Contact>();
  21. Map<String, User> UserMap = new Map<String, User>();
  22. Map<Id, SFDC_Employee__c> EmployeeMap = new Map<Id, SFDC_Employee__c>();
  23. Map<String, SFDC_Employee__c> EmployeeMapWithEvent = new Map<String, SFDC_Employee__c>();
  24. Map<String, String> ContentDocumentMap = new Map<String, String>();
  25. List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
  26. for(Event e : EventDetails.values()) {
  27. //Don't Add Lead Records
  28. if(e.WhoId.getSObjectType().getDescribe().getName() == 'Contact') {
  29. WhoIDs.add(e.WhoId);
  30. }
  31. WhatIDs.add(e.WhatId);
  32. UserIds.add(e.OwnerId);
  33. }
  34. List<User> Users = new List<User> ([SELECT Id, Scheduler__c, RepPhotoDocumentID__c, Phone, Email, Name from User WHERE Id IN: UserIds]);
  35. List<SFDC_Employee__c> Employees = new List<SFDC_Employee__c> ([Select ID, Employee_User__c From SFDC_Employee__c WHERE Employee_User__c IN: UserIds]);
  36. List<Contact> Contacts = new List<Contact> ([SELECT ID, FirstName, Email FROM Contact WHERE ID IN: WhoIDs]);
  37. List<Opportunity> Opportunities = new List<Opportunity> ([SELECT ID, Record_Type_Name__c, Lead_Record_Type__c FROM Opportunity WHERE ID IN: WhatIDs]);
  38.  
  39. for(Event e : EventDetails.values()) {
  40. for(Opportunity opp : Opportunities) {
  41. if(opp.Id == e.WhatId) {
  42. OpportunityMap.put(e.Id, opp);
  43. }
  44. }
  45. for(Contact con : Contacts) {
  46. if(con.Id == e.WhoId) {
  47. ContactMap.put(e.Id, con);
  48. }
  49. }
  50. for(User u : Users) {
  51. UserMap.put(e.id, u);
  52. for(SFDC_Employee__c employee : Employees) {
  53. if(employee.Employee_User__c == u.id) {
  54. EmployeeMap.put(employee.id, employee);
  55. EmployeeMapWithEvent.put(e.id, employee);
  56. }
  57. }
  58. }
  59. }
  60. List<ContentVersion> convers = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Title =: 'EmployeePhoto.jpg' AND Description IN: EmployeeMap.keySet() AND IsLatest = TRUE];
  61.  
  62. for(Event e : EventDetails.values()) {
  63. for(ContentVersion version : convers) {
  64. if(version.Description == EmployeeMapWithEvent.get(e.Id).Id) {
  65. ContentDocumentMap.put(e.id, version.ContentDocumentId);
  66. }
  67. }
  68. }
  69. List<ContentDistribution> ContentDistributionList = [Select ContentDownloadUrl from ContentDistribution WHERE ContentDocumentID IN : ContentDocumentMap.values()];
  70.  
  71. for(Event e : EventDetails.values()) {
  72. for(ContentDistribution cd : ContentDistributionList) {
  73. if(cd.ContentDocumentId == ContentDocumentMap.get(e.id)) {
  74. SalesRepPhotoURL = cd.ContentDownloadUrl;
  75. System.debug(LoggingLevel.DEBUG, 'Sales Rep Photo URL:' + SalesRepPhotoURL);
  76. }
  77. }
  78.  
  79. SalesRepName = UserMap.get(e.id).Name;
  80. SalesRepEmail = UserMap.get(e.id).Email;
  81.  
  82. Name = ContactMap.get(e.Id).FirstName;
  83. Email = ContactMap.get(e.Id).Email;
  84.  
  85.  
  86. if(OpportunityMap.get(e.Id).Lead_Record_Type__c != OpportunityMap.get(e.Id).Record_Type_Name__c && e.CreatedDate.addMinutes(5) <= DateTime.now()) {
  87. errorFlag = true;
  88. }
  89. if (OpportunityMap.get(e.Id).Record_Type_Name__c == 'Home Depot') {
  90. OrgWideEmailAddress = HDOrgEmailAddress;
  91. Company = 'Home Depot Fence Services';
  92. LogoURL = '"https://drive.google.com/uc?export=download&id=1Fa0C2tvvguF3HOU311v_IYlnHem6mO6c"';
  93. }
  94.  
  95.  
  96. if (Email != NULL && errorFlag == false) {
  97. // Set Single Email Variables that are added to a list to be mass sent in one call
  98. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  99. mail.setOrgWideEmailAddressId(OrgWideEmailAddress);
  100. mail.setWhatId(e.WhatId);
  101. mail.setTargetObjectId(e.WhoId);
  102. String subject='Appointment Confirmation';
  103. String body='<html><head>' + '</head><body><p style="color: #000000;">Hello ' + Name + ', <br><br>Thank you for your interest in working with ' + Company + '. <br> Your appointment is on ' + e.StartDateTime.format('MMMM dd, yyyy') + ' at ' + e.StartDateTime.format('h:mm a') + '. To reschedule, please call <a href="tel:6305847710">(630) 584-7710</a> </p><br><img width="180px" src=' + LogoURL + '>' + '</body></html>';
  104. mail.setHtmlBody(body);
  105. mail.setSubject(subject);
  106. mail.setSaveAsActivity(TRUE);
  107. allMails.add(mail);
  108. e.Confirmation_Sent__c = TRUE;
  109.  
  110. }
  111. }
  112. // Array of mails to send.
  113. Messaging.sendEmail(allMails);
  114. update EventDetails.values();
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement