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
Chinna SfdcChinna Sfdc 

Need help for Batch Apex

Hi Team,

We have created Two batch classes using different objects(Account and Opportunity) for updating the Teammembrs which are working fine.But my problem is i want to include onebatch class instead of two batch class.Can any one please help on this.

Thanks
Amit GhadageAmit Ghadage
Hi Chinna,
Use  an Iterable in Batch Apex to Define Scope
Refere below code
global class batchClass implements Database.batchable<SObject>{ 
   
   global Iterable<Sobject> start(Database.batchableContext info){ 
       Iterable<SObject> iterator = (Iterable<SObject>) new CustomIterator();
       return iterator; 
   }     
   
   global void execute(Database.batchableContext info, List<SObject> scope){ 
       for(Sobject s : scope)
       {
          if(s.getSObjectType() == 'Account')
          // account processing
          if(s.getSObjectType() == 'Contact')
             // contact processing

       }
   }     
   global void finish(Database.batchableContext info){     
   } 
}

global class CustomIterator implements Iterator<SObject>{ 
   
   List<SObject> soList {get; set;} 
   Integer i {get; Set;}
   public CustomIterator(){ 
       List<Account> a = [Select Id,name from Account Limit 2];
       soList= new List<SObject>();
       soList.addAll((List<SObject>)a);
       List<Contact> b = [Select Id,name from Contact limit 2];
       soList.addAll((List<SObject>)b);
       System.debug('soList'+soList);
       i=0;
   }   
    
     global boolean hasNext(){ 
      if(i >= soList.size()) { return false; }
       else { return true; } }
      global SObject next()
       {  if(i == soList.Size())
           {return null;}
            i++;
            return soList[i-1]; }
 }
   

Best Regards,
Amit Ghadage
sandeep reddy 37sandeep reddy 37
Is it working fine  how we check object type you not used lolist then how can we fount it how iterater saperate account and contact why you used getnext() method and hasnext() also we are not used  this methodshow to acomplish this