+ Start a Discussion
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12 

How to send the exception email notification?

Hi

Kindly refer the following code,I have written sheduller class to deleteing Test record, need to send the email notification

Code
==============
global class TestRecordManager implements Schedulable{
    global void execute(SchedulableContext sc){ 
    try{
        //Get the Technician order records
        List<Technician_Orders__c> Technician =[SELECT Id,name FROM Technician_Orders__c WHERE Site_Address__r.Parent.Name LIKE 'Salesforce Test Customer%' OR Site_Address__r.name LIKE 'Salesforce Test Customer%' OR Site_Address__r.name LIKE 'Training%'];
        if(Technician.size() > 0)
        Delete Technician;
        
        //Get the supplier records
        List<SFDC_Purchase_Order__c> Supplier =[SELECT Id,name FROM SFDC_Purchase_Order__c WHERE Visit__r.Site__r.Parent.Name LIKE 'Salesforce Test Customer%' OR Visit__r.Site__r.Name LIKE 'Salesforce Test Customer%' OR Visit__r.Site__r.Name LIKE 'Training%'];
        if(Supplier.size() > 0)
        Delete Supplier; 
        
        //Get the SiteContract records
        List<SiteContracts__c> Sitecontract =[SELECT Id,name FROM SiteContracts__c WHERE  Parent_from_Site__c LIKE 'Salesforce Test Customer%' OR Site__r.name LIKE 'Salesforce Test Customer%' OR Site__r.name LIKE 'Training%'];
        if(Sitecontract.size() > 0)
        Delete Sitecontract;
        
        //Get the Visit records
        List<Visits__c> Visit =[SELECT Id,name FROM Visits__c WHERE  Parent_Account__c LIKE 'Salesforce Test Customer%' OR Site__r.name LIKE 'Salesforce Test Customer%' OR Site__r.name LIKE 'Training%'];
        if(Visit.size() > 0)
        Delete Visit;
       
       }
      catch(Exception e) {
      String msg=e.getMessage();    // How to send the email notification to user?

      
      }
    }

Thanks.
 }
Best Answer chosen by sundhar.mks1.3962649227519546E12
Tejpal KumawatTejpal Kumawat
Hello Friend,

Try this in your catch block :
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Support Email Id
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Error Support');
mail.setSubject('Error in code ');
String mailBody = ''+ msg;
mail.setPlainTextBody(mailBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
	mail
});
If this answers your question then hit Like and mark it as solution!