I copied this code from SF docs and modified it to my requirement:
global class OwnerReassignment implements Database.Batchable<sObject>{
global final string query;
global final string email;
/*global OwnerReassignment (String q, String e){
query=q;
email=e;
}*/
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Lead> leads = new List<Lead>();
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
for(sObject s : scope){
Lead a = (Lead)s;
a.setOptions(dmo);
a.Notes__c='Test Execute Batch Class';
leads.add(a);
}
update leads;
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
mail.setReplyTo('batch@acme.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
When I execute this Code it is not updating record.
OwnerReassignment reassign = new OwnerReassignment();
reassign.query = 'SELECT Id, OwnerId FROM Lead WHERE OwnerId = \'00Q2000000nCIJo\'';
reassign.email='abc@abc.com';
ID batchprocessid = Database.executeBatch(reassign);
Can anyone suggest what is wrong?
globalmodifier. So, anyone who copying the code their will have theglobalmodifier. Regardless of why it shouldglobal. Batches should not useglobalunless it is required. – Ashwani Mar 03 '16 at 15:37