+ Start a Discussion
swathi medak 9swathi medak 9 

calling class from execute fuction in batchclass

Hi,
plz tell me whats wrong with my code:
global class batchaccountdescupdate implements database.batchable<sObject>,database.stateful{
global Database.QueryLocator start(Database.BatchableContext BC){
string query='SELECT Description FROM Account';
return Database.getQueryLocator(query);
}
global void execute(database.batchablecontext BC, list<Account> scope){
for(Account a: scope){
if(a.Description=='Not Available')
a.Description='Please fill details, else record will be made inactive';
}
update scope;
}
global void finish(database.batchablecontext BC,list<Account> scope){
integer totalemployeecount=0;
for(Account a: scope){
totalemployeecount=totalemployeecount+a.NumberOfEmployees;
}
update scope;
}
}
here is the error im getting:
Error: Compile Error: Class batchaccountdescupdate must implement the method: void Database.Batchable<SObject>.finish(Database.BatchableContext) at line 1 column 14
thanks.
Best Answer chosen by swathi medak 9
Prakhar Saxena 19Prakhar Saxena 19
Hi Swathi,

Move your code logic from the finish method to the execute method. Start and finish methods just run once. So, any logic that involves iterating over a list of records should be put in the execute method. Also, if you are using NumberOfEmployees field, you need to add the same in the query of the start method.

Use Batch Apex (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch)

You can try something like this:
 
global class BatchAccountDescUpdate implements Database.Batchable<sObject>,Database.Stateful{
    
    global Integer totalemployeecount=0;
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query='SELECT Description, NumberOfEmployees FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.Batchablecontext BC, List<Account> scope){
        for(Account a: scope){
            if(a.Description=='Not Available'){
                a.Description='Please fill details, else record will be made inactive';
            }  
            totalemployeecount = totalemployeecount + a.NumberOfEmployees;
        }
        update scope;
    }
    global void finish(Database.Batchablecontext BC){
        System.debug('totalemployeecount: '+totalemployeecount);
    }
}




 

All Answers

Prakhar Saxena 19Prakhar Saxena 19
Hi Swathi,

Move your code logic from the finish method to the execute method. Start and finish methods just run once. So, any logic that involves iterating over a list of records should be put in the execute method. Also, if you are using NumberOfEmployees field, you need to add the same in the query of the start method.

Use Batch Apex (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch)

You can try something like this:
 
global class BatchAccountDescUpdate implements Database.Batchable<sObject>,Database.Stateful{
    
    global Integer totalemployeecount=0;
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query='SELECT Description, NumberOfEmployees FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.Batchablecontext BC, List<Account> scope){
        for(Account a: scope){
            if(a.Description=='Not Available'){
                a.Description='Please fill details, else record will be made inactive';
            }  
            totalemployeecount = totalemployeecount + a.NumberOfEmployees;
        }
        update scope;
    }
    global void finish(Database.Batchablecontext BC){
        System.debug('totalemployeecount: '+totalemployeecount);
    }
}




 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Swathi,

You can try this.

global class batchaccountdescupdate implements Database.Batchable<sObject>,database.stateful{
    global Database.QueryLocator start(Database.BatchableContext BC){
        string query='SELECT Description FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, list<Account> scope){
        for(Account a: scope){
            if(a.Description=='Not Available')
                a.Description='Please fill details, else record will be made inactive';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC){
        
    }
}


Note- If you want to do this operation then write this code inside the execute method.

integer totalemployeecount=0;
for(Account a: scope){
    totalemployeecount=totalemployeecount+a.NumberOfEmployees;
}
    update scope;


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Swathi,
Greetings to you

I have implemented your code and the error is showing because you have written a second parameter list<Account> scope in finish method.
You cannot give a second parameter in finish method because finish method is a single parameterised metrhod.
 
global class batchaccountdescupdate implements database.batchable<sObject>,database.stateful{
global Database.QueryLocator start(Database.BatchableContext BC){
string query='SELECT Description FROM Account';
return Database.getQueryLocator(query);
}
global void execute(database.batchablecontext BC, list<Account> scope){
for(Account a: scope){
if(a.Description=='Not Available')
a.Description='Please fill details, else record will be made inactive';
}
update scope;
}
global void finish(database.batchablecontext BC){
/*integer totalemployeecount=0;
for(Account a: scope){
totalemployeecount=totalemployeecount+a.NumberOfEmployees;
}
update scope;*/
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha