• Knowlegde Base
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
global class LeadProcessor implements 
    Database.Batchable<Leads>, Database.Stateful {
//     Database.Batchable<Sobject>, Database.Stateful {

    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([Select LastName From Leads ]);

     }
          
    }

    global void execute(Database.BatchableContext bc, List<Leads> scope){
        // process each batch of records
        List<Leads> leads = new List<Leads>();
            for (Leads:leads) {
                Leads.LeadSource = Dreamforce;
                // add Leads to the list to be updated
                leads.add(leads);
                // increment the instance member counter
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update leads;
    }    

    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        // call some utility to send email
        EmailUtils.sendMessage(a, recordsProcessed);
    }    

}