Sie sind auf Seite 1von 1

global class BatchContactMail implements Database.

Batchable<sObject> { global string query='select id from Contact where Email_Opt__c = true '; global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Contact> scope){ List<Id> contactids = new List<Id>(); for(Contact s : scope){ contactids.add(s.id); } Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage (); mail.setTargetObjectIds(contactids); mail.setTemplateId('00X90000000l060'); Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail }); } global void finish(Database.BatchableContext BC){ // Get the ID of the AsyncApexJob representing this batch job // from Database.BatchableContext. // Query the AsyncApexJob object to retrieve the current job's i nformation. AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsPro cessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job co mpletion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMes sage(); String[] toAddresses = new String[] {a.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Apex Sharing Recalculation ' + a.Status); mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJ obItems + ' batches with '+ a.NumberOfErrors + ' failures. '); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }) ; } }

Das könnte Ihnen auch gefallen