1

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?

C0DEPirate
  • 1,599
  • 21
  • 42
  • 1
    Have you created a test class for this? You'll learn a great deal from using Test Driven Development. I also highly recommend How do I start to debug my own Apex code?. I don't see any debug statements in your code to help you figure out the source of your problem. – crmprogdev Mar 03 '16 at 15:32
  • 2
    ProTip: don't actually make this global unless you intend put it in a managed package AND intend to call it from outside that package. I don't understand why SFDC's examples so frequently show globals, given how strict they are about actually using them. – RenegadeCoder Mar 03 '16 at 15:33
  • In Salesforce example template, they use global modifier. So, anyone who copying the code their will have the global modifier. Regardless of why it should global. Batches should not use global unless it is required. – Ashwani Mar 03 '16 at 15:37

1 Answers1

2

Verify that Lead has some records or not. Debugging is good way to find out the problem.

OwnerReassignment reassign = new OwnerReassignment();

reassign.query = 'SELECT Id, OwnerId FROM Lead WHERE OwnerId = \'00Q2000000nCIJo\'';
// Check
List<Lead> leadList = Database.query(reassign.query);
System.debug(' List Size ' +leadList.size());
// Remove above two lines if it works.

reassign.email='abc@abc.com';
ID batchprocessid = Database.executeBatch(reassign);

In addition this will also work:-

      for(Lead a : (List<Lead>)scope)
      {
            a.setOptions(dmo);
            a.Notes__c='Test Execute Batch Class';
      }

      update scope;
Ashwani
  • 22,582
  • 4
  • 38
  • 72