Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class ImmediateAppointmentConfirmation implements Schedulable {
- global void execute(SchedulableContext ctx) {
- if (DateTime.now().hour() >= 8 && DateTime.now().hour() <= 20 || Test.isRunningTest()) {
- Boolean errorFlag = false;
- String Email;
- String Name = '';
- String contentDocumentID;
- String SalesRepPhotoURL;
- String SalesRepName;
- String SalesRepEmail;
- String Company = 'Peerless Fence';
- String OrgWideEmailAddress = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: 'Peerless Fence Group'].Id;
- String HDOrgEmailAddress = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName =: 'Home Depot Fence Service'].Id;
- String LogoURL = '"https://peerlessfence.com/sites/default/files/logo-01.png"';
- Set <Id> WhoIDs = new Set <Id>();
- Set <Id> WhatIDs = new Set <Id>();
- Set <Id> UserIds = new Set <Id>();
- 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]);
- Map<String, Opportunity> OpportunityMap = new Map<String, Opportunity>();
- Map<String, Contact> ContactMap = new Map<String, Contact>();
- Map<String, User> UserMap = new Map<String, User>();
- Map<Id, SFDC_Employee__c> EmployeeMap = new Map<Id, SFDC_Employee__c>();
- Map<String, SFDC_Employee__c> EmployeeMapWithEvent = new Map<String, SFDC_Employee__c>();
- Map<String, String> ContentDocumentMap = new Map<String, String>();
- List<Messaging.SingleEmailMessage> allMails = new List<Messaging.SingleEmailMessage>();
- for(Event e : EventDetails.values()) {
- //Don't Add Lead Records
- if(e.WhoId.getSObjectType().getDescribe().getName() == 'Contact') {
- WhoIDs.add(e.WhoId);
- }
- WhatIDs.add(e.WhatId);
- UserIds.add(e.OwnerId);
- }
- List<User> Users = new List<User> ([SELECT Id, Scheduler__c, RepPhotoDocumentID__c, Phone, Email, Name from User WHERE Id IN: UserIds]);
- 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]);
- List<Contact> Contacts = new List<Contact> ([SELECT ID, FirstName, Email FROM Contact WHERE ID IN: WhoIDs]);
- List<Opportunity> Opportunities = new List<Opportunity> ([SELECT ID, Record_Type_Name__c, Lead_Record_Type__c FROM Opportunity WHERE ID IN: WhatIDs]);
- for(Event e : EventDetails.values()) {
- for(Opportunity opp : Opportunities) {
- if(opp.Id == e.WhatId) {
- OpportunityMap.put(e.Id, opp);
- }
- }
- for(Contact con : Contacts) {
- if(con.Id == e.WhoId) {
- ContactMap.put(e.Id, con);
- }
- }
- for(User u : Users) {
- UserMap.put(e.id, u);
- for(SFDC_Employee__c employee : Employees) {
- if(employee.Employee_User__c == u.id) {
- EmployeeMap.put(employee.id, employee);
- EmployeeMapWithEvent.put(e.id, employee);
- }
- }
- }
- }
- List<ContentVersion> convers = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Title =: 'EmployeePhoto.jpg' AND Description IN: EmployeeMap.keySet() AND IsLatest = TRUE];
- for(Event e : EventDetails.values()) {
- for(ContentVersion version : convers) {
- if(version.Description == EmployeeMapWithEvent.get(e.Id).Id) {
- ContentDocumentMap.put(e.id, version.ContentDocumentId);
- }
- }
- }
- List<ContentDistribution> ContentDistributionList = [Select ContentDownloadUrl from ContentDistribution WHERE ContentDocumentID IN : ContentDocumentMap.values()];
- for(Event e : EventDetails.values()) {
- for(ContentDistribution cd : ContentDistributionList) {
- if(cd.ContentDocumentId == ContentDocumentMap.get(e.id)) {
- SalesRepPhotoURL = cd.ContentDownloadUrl;
- System.debug(LoggingLevel.DEBUG, 'Sales Rep Photo URL:' + SalesRepPhotoURL);
- }
- }
- SalesRepName = UserMap.get(e.id).Name;
- SalesRepEmail = UserMap.get(e.id).Email;
- Name = ContactMap.get(e.Id).FirstName;
- Email = ContactMap.get(e.Id).Email;
- if(OpportunityMap.get(e.Id).Lead_Record_Type__c != OpportunityMap.get(e.Id).Record_Type_Name__c && e.CreatedDate.addMinutes(5) <= DateTime.now()) {
- errorFlag = true;
- }
- if (OpportunityMap.get(e.Id).Record_Type_Name__c == 'Home Depot') {
- OrgWideEmailAddress = HDOrgEmailAddress;
- Company = 'Home Depot Fence Services';
- LogoURL = '"https://drive.google.com/uc?export=download&id=1Fa0C2tvvguF3HOU311v_IYlnHem6mO6c"';
- }
- if (Email != NULL && errorFlag == false) {
- // Set Single Email Variables that are added to a list to be mass sent in one call
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- mail.setOrgWideEmailAddressId(OrgWideEmailAddress);
- mail.setWhatId(e.WhatId);
- mail.setTargetObjectId(e.WhoId);
- String subject='Appointment Confirmation';
- 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>';
- mail.setHtmlBody(body);
- mail.setSubject(subject);
- mail.setSaveAsActivity(TRUE);
- allMails.add(mail);
- e.Confirmation_Sent__c = TRUE;
- }
- }
- // Array of mails to send.
- Messaging.sendEmail(allMails);
- update EventDetails.values();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement