calling batch apex from class

Use database.execute method to call a batch apex from a class.

Batch apex class

global class BatchApexDemo implements database.batchable<sobject>{
Public string soqlquery;
 Public void setQry(string soqlquery){
    this.soqlquery = 'Select name,status from account limit 1';
 global database.querylocator start(database.batchableContext bc){
  return database.getquerylocator(soqlquery);
 global void execute(database.batchablecontext bd, list<sObject> sc){
   Account ac = sc[0];
  ac.status = active;
  update ac;
 Public void finish(database.batchableContext bc){

Class that calls the above batch apex

Public Class callBatchApexclass{
 Public callBatchApexclass(){
   database.executebatch(new BatchApexDemo(),200);

Execute the above class from developer console. Set up -> developer console -> debug --> open execute anonymous window -> write your code --> execute

Developer console code
callBatchApexclass cBa = new callBatchApexclass();

No comments:

Post a comment