function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhruv NirgudeDhruv Nirgude 

I created one BatchApex Class and i got some errors.

Here is my Batch Class. Below the class i mentioned errors as well.

global Class BatchApexContact Implements Database.Batchable<sobject>, Database.Stateful{
    //Global Class BatchApexContact Implements Database.Batchable<sobject> {
    
    Integer RecordProcessed = 0;
    Integer RecordFailed = 0;
    List<Contact> lstCon = new List<Contact>();
    Integer NoOfTran = 0;
    
    //start method
    public Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select id, FirstName, LastName from Contacts where FirstName like ' mm1']);
    }      
    public void execute (Database.BatchableContext bc, List<Contact> con){
        List<Contact> lstConFinal = new List<Contact>();
        for(Contact C: con){
            //Scenario Unsucessful Transaction
            if(c.firstName ==mm1 && c.Lastname == 'nnn1'){
                RecordProcessed = RecordProcessed+1;
                c.firstname = A1;
                c.LastName = B2;  
                lstConFinal.add(C);
                lstCon.add(C);
            }  else {
                RecordFailed =  RecordFailed+1;
            }
        }    
        Update lstConFinal;
        NoOfTran = con.size();
        
        //end of Execute 
        
        // Finish Method
        Public void finish (Database.BatchableContext bc){
            AsyncApexJob job = [Select Id ,CreatedDate, CreatedByIdDate, JobType, ApexClassId, Status, JobItemsProcessed,
                                TotalJobItems, NumberOfErrors, CompletedDate, MethodName,
                                ExtendedStatus From AsyncApexJob where Id=: bc.getJobId()];
            System.debug('*** JobId: '+bc.getJobId());
            System.debug('*** job:' +job);
            System.debug('*** Total no of Transactions: '+NoOfTran);   
            System.debug('*** Record Processed: ' +RecordProcessed);
            System.debug('*** No of Records Failed : '+RecordFailed);
            System.debug('*** List Of records Processed:' +lstCon);
            System.debug('***How Many recs were Processed:' +lstCon.size());
        }
    }    
}

1 Class BatchApexContact must implement the method: void Database.Batchable<SObject>.finish(Database.BatchableContext)

2Unexpected token 'Public'.
3Missing ';' at 'finish'
4Extra ')', at 'bc'.

        

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Druv,

try with below code.
 
global Class BatchApexContact Implements Database.Batchable<sobject>, Database.Stateful{
    //Global Class BatchApexContact Implements Database.Batchable<sobject> {
    
    Integer RecordProcessed = 0;
    Integer RecordFailed = 0;
    List<Contact> lstCon = new List<Contact>();
    Integer NoOfTran = 0;
    
    //start method
    public Database.QueryLocator start(Database.BatchableContext bc){
        string serchkey = 'mm1';
        string query = 'Select id, FirstName, LastName from Contactst WHERE FirstName LIKE \'%' + serchkey + '%\'';
        return Database.getQueryLocator(query);
    }      
    public void execute (Database.BatchableContext bc, List<Contact> con){
        List<Contact> lstConFinal = new List<Contact>();
        for(Contact C: con){
            //Scenario Unsucessful Transaction
            if(c.firstName =='mm1' && c.Lastname == 'nnn1'){
                RecordProcessed = RecordProcessed+1;
                c.firstname = 'A1';
                c.LastName = 'B2';  
                lstConFinal.add(C);
                lstCon.add(C);
            }  else {
                RecordFailed =  RecordFailed+1;
            }
        }    
        Update lstConFinal;
        NoOfTran = con.size();
    }       
        //end of Execute 
        
        // Finish Method
        Public void finish (Database.BatchableContext bc){
            AsyncApexJob job = [Select Id ,CreatedDate, JobType, ApexClassId, Status, JobItemsProcessed,
                                TotalJobItems, NumberOfErrors, CompletedDate, MethodName,
                                ExtendedStatus From AsyncApexJob where Id=:bc.getJobId()];
            System.debug('*** JobId: '+bc.getJobId());
            System.debug('*** job:' +job);
            System.debug('*** Total no of Transactions: '+NoOfTran);   
            System.debug('*** Record Processed: ' +RecordProcessed);
            System.debug('*** No of Records Failed : '+RecordFailed);
            System.debug('*** List Of records Processed:' +lstCon);
            System.debug('***How Many recs were Processed:' +lstCon.size());
        }
        
}

If this helps, Please mark it as best answer.

Thanks!!