You need to sign in to do that
Don't have an account?
Please critque my code - send email via a trigger
New Apex developer, old IBM AS400 RPG programmer. Don't yet understand OO looking to see if I'm going about things okay.
Not sure when I should be calling Static vs Instantiated methods.
Trigger on Booking Object AKA Community_Visit
Not sure when I should be calling Static vs Instantiated methods.
Trigger on Booking Object AKA Community_Visit
trigger BookingTrigger on Community_Visit__c (before insert, before update, after insert, after update) { BookingTriggerHandler handler = new BookingTriggerHandler(); if(Trigger.isInsert){ if(Trigger.isBefore){ handler.onBeforeInsert(Trigger.new); } if(Trigger.isAfter){ handler.onAfterInsert(Trigger.new); } } if(Trigger.isUpdate){ if(Trigger.isBefore){ handler.onBeforeUpdate(Trigger.old,Trigger.new,Trigger.oldMap); } if(Trigger.isAfter){ handler.onAfterUpdate(Trigger.old,Trigger.new); } } }Booking Trigger Handler
/************************************************************************************** Class Name : BookingTriggerHandler Created By : Eric Created on : 08/04/2014 Description : Booking ( Community_Visit__C ) Trigger Handler Version : 1.0 ***************************************************************************************/ public with sharing class BookingTriggerHandler { /* When Created */ public void onBeforeInsert(list<Community_Visit__c> newCV){ System.debug('BK before insert'+newCV); List<Community_Visit__c> CVtoEm = new List<Community_Visit__c>(); Set<Id> CVAccts = new Set<Id>(); Set<Id> CVUsers = new Set<Id>(); for (Community_Visit__c CV : newCV){ if(CV.Type__c == '3 Day' && CV.Total_Billable_Amount__c > 0){ CV.Type_of_Funds__c = 'With Funds'; } else { CV.Type_of_Funds__c = 'With Out Funds'; } if(CV.Type__c == '3 Day' && CV.Status__c == 'Pending Billing' && CV.Date__c > Date.today().addDays(30)){ CVtoEm.add(CV); } } if(CVtoEm.size()>0){ BookingEmailToCustNewAppt.sendCustEmail(CVtoEM); //bookingEmailToCustNewAppt be = new bookingEmailToCustNewAppt(); //be.SendCustEmail(CVtoEm); } } public void onAfterInsert(list<Community_Visit__c> newCV){ System.debug('BK after insert'+newCV); for (Community_Visit__c CV : newCV){ if(CV.Type__c == '3 Day' && CV.Total_Billable_Amount__c > 0){ DNCTransactions.addExceptionforBooking(CV.Account__c,CV); } } } /* When Updated */ public void onBeforeUpdate(list<Community_Visit__c> oldCV, list<Community_Visit__c> newCV, map<Id,Community_Visit__c> MoldCV){ System.debug('BK before update'+newCV); List<Community_Visit__c> CVtoEm = new List<Community_Visit__c>(); for (Community_Visit__c CV : newCV){ if(CV.Type__c == '3 Day' && CV.Total_Billable_Amount__c > 0){ CV.Type_of_Funds__c = 'With Funds'; } else { CV.Type_of_Funds__c = 'With Out Funds'; } if(CV.Type__c == '3 Day' && CV.Status__c == 'Pending Billing' && CV.Status__c != MoldCV.get(CV.Id).Status__c && CV.Date__c > Date.today().addDays(30)){ CVtoEm.add(CV); } } System.debug('CVtoEm='+CVtoEm+' size='+CVtoEm.size()); if(CVtoEm.size()>0){ BookingEmailToCustNewAppt.sendCustEmail(CVtoEM); } } public void onAfterUpdate(list<Community_Visit__c> oldCV, list<Community_Visit__c> newCV){ System.debug('BK after update'+newCV); for (Community_Visit__c CV : newCV){ if(CV.Type__c == '3 Day' && CV.Total_Billable_Amount__c > 0){ system.debug('the fund value is '+CV.Type_of_Funds__c); DNCTransactions.addExceptionforBooking(CV.Account__c,CV); } } } }BookingEmailToCustNewAppt
/******************************************************* ClassName: BookingEmailToCustNewAppt Created By: Eric Created Date: 08/01/2014 Description: Send Appt email to customer ********************************************************/ public with sharing class BookingEmailToCustNewAppt { public static void SendCustEmail(list <Community_Visit__c> listCVs){ System.debug('In SendCustEmail'); // Step 0: Create a master list to hold the emails we'll send List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); Set<Id> CVAccts = new Set<Id>(); Set<Id> CVUsers = new Set<Id>(); for (Community_Visit__c CV : listCVs){ CVAccts.add(CV.Account__c); CVUsers.add(CV.InsideSalesperson__c); } Map<Id,Account> AcctM = new Map<Id,Account>([SELECT Id, PersonEmail FROM Account WHERE Id IN :CVAccts]); System.debug('AcctM='+AcctM); Map<Id,User> UserM = new Map<Id,User>([SELECT Id, Name,Email,Signature FROM User WHERE Id IN :CVUsers]); System.debug('UserM='+UserM); for (Community_Visit__c CV : listCVs){ System.debug('CV='+CV); datetime aDt = CV.Date__c; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); List<String> sendTo = new List<String>(); if (CV.Alternate_Email__c != null) { sendTo.add(CV.Alternate_Email__c); } else { sendTo.add(AcctM.get(CV.Account__c).PersonEmail); } System.debug('sendTo=' +sendTo); mail.setToAddresses(sendTo); mail.setReplyTo(UserM.get(CV.InsideSalesperson__c).Email); mail.setSenderDisplayName(UserM.get(CV.InsideSalesperson__c).Name); // List<String> BccTo = new List<String>(); // BccTo.add(UserM.get(CV.InsideSalesperson__c).Email); // mail.setBccAddresses(BccTo); mail.setSubject('Confirmation of your Vist'); String body = '<p>Dear ' +CV.First_Name__c+ ',</p>'; body += '<p>Text</p>'; body += '<p>More text.</p>'; body += '<p>A dollar amount $' +CV.Total_Billable_Amount__c.SetScale(2)+ ' (includes tax) at that time. '; body += 'are scheduled to arrive on ' +aDt.format('EEEE, MMMM d, yyyy')+ '.'; body += 'If you have any questions, please feel free to call the ' +CV.Community_Names__c+ ' sales office toll-free at '; body += '' +CV.Cmty_Phone__c+ '.</p>'; body += '<br>Sincerely,'; if (UserM.get(CV.InsideSalesperson__c).Signature <= ''){ body += '<br><br>'+ UserM.get(CV.InsideSalesperson__c).Name+ '<br>'; body += 'Inside Sales Consultant<br>'; } mail.setHtmlBody(body); mails.add(mail); } System.debug('mails='+mails); try{ Messaging.sendEmail(mails); } catch(exception ex1) { System.debug('ex1='+ex1); Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'eric.@****.com'}; mail.setToAddresses(toAddresses); mail.setReplyTo('eric@****.com'); mail.setSenderDisplayName('Apex error message'); mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName()); mail.setPlainTextBody(ex1.getMessage()); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } }
Can you please your query more clear?
Regarding use of static methods below links will be helpful.Else clarify your query.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex5_4.htm
Please refer this.
Regards
Nitesh