Advertisement
Kenji776

Batchable

Jan 17th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //GETS CALLED BY: id batchinstanceid = database.executeBatch(new deleteCampaignMembers(), 400);  
  2.  
  3. global class deleteCampaignMembers implements Database.Batchable<sObject>
  4. {
  5.     global String Query;
  6.     global integer numRecords = 0;
  7.     global void deleteCampaignMembers()
  8.     {
  9.         Query='select id from campaignMember where createdDate > LAST_90_DAYS';
  10.     }
  11.  
  12.     global Database.QueryLocator start(Database.BatchableContext BC)
  13.     {
  14.         return Database.getQueryLocator(query);
  15.     }
  16.  
  17.     global void execute(Database.BatchableContext BC,List<sObject> scope)
  18.     {
  19.         numRecords += scope.size();
  20.         Delete scope;
  21.     }
  22.  
  23.     global void finish(Database.BatchableContext BC)
  24.     {
  25.         //Send an email to the User after your batch completes
  26.         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  27.         String[] toAddresses = new String[] {'dllewellyn@foodperspectives.com'};
  28.         mail.setToAddresses(toAddresses);
  29.         mail.setSubject('Apex bulk delete campaign members complete');
  30.         mail.setPlainTextBody('The batch Apex job processed. Deleted  ' + numRecords + ' campaign members');
  31.         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement