+ Start a Discussion
D J RobertsD J Roberts 

How to use System.Debug in Batch Class?

Trying to check the query results in a Batch Class, but not sure what to call. The code is below. Any help would be great. Again, just trying to log the query result within a batch class. 
 
global class LeadProcessor implements Database.Batchable<sobject>, Database.Stateful  {
    global Database.QueryLocator start(Database.BatchableContext bc){
            String query = 'Select Id, LeadSource  FROM Lead WHERE LeadSource != \'DreamForce\'' ; 
             
            return Database.getQueryLocator(query); 
    }

    global void execute(Database.BatchableContext bc, List<Lead> records){
        System.debug('Query Results = ' + bc()); 
        // process each batch of records
    }        
        

    global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    

    
}

 
Abdul KhatriAbdul Khatri
There was only one issue in your code under the execute method where you are using bc() in system.debug. Here is the code with little change for little cleaner
 
global class LeadProcessorBatch implements Database.Batchable<sObject>  {

    global String query;

    global LeadProcessorBatch(){
       query = 'Select Id, LeadSource  FROM Lead LIMIT 100';// WHERE LeadSource != \'DreamForce\'' ; 
       system.debug('query2 : ' + query);       IN 
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query); 
    }

    global void execute(Database.BatchableContext bc, List<Lead> records){

        system.debug('Query Results = ' + records); 
        // process each batch of records
        
    }        
        
    global void finish(Database.BatchableContext bc){
        system.debug('finish');
        // execute any post-processing operations
    }    

}
In debug log you probably able to see it once you open that file as shown in the screen shot below

User-added image

Let me know if this help and don't forget to mark it best if helped.