2

Only one object is updating using this method, how to update more than one object?

    public with sharing class Batch_act implements Database.Batchable < sObject > , Database.Stateful {
                private Integer currentBatchChain;
                public boolean bReRun = false;
                public Batch_act() {
                    currentBatchChain = 1;
                }

                public Batch_act(Integer chain) {
                    currentBatchChain = chain;
                }

                public Database.QueryLocator start(Database.BatchableContext BC) {
                    String qry;

                    if (currentBatchChain == 1) {
                        qry = 'Select Id, Test__c From Obj1 Where name != \'P\'';
                    } else if (currentBatchChain == 2) {
                        qry = 'Select Id, Test__c From Obj2 Where name != \'P\'';
                    } else if (currentBatchChain == 3) {
                        qry = 'Select Id, Test__c From Obj3 Where name != \'P\'';
                    }
                    return Database.getQueryLocator(qry);
                }


                public void execute(Database.BatchableContext BC, List < sObject > scope) {
                    List < obj1 > listToUpdate = new List<obj1>();
                    for( Obj1  o1: (List<obj1>)scope)
                    { 
                        if (o1.Test__c == true){
                            o1.Test__c = false;
                        }else {
                                o1.Test__c = true;
                              }

                        listToUpdate.add(o1);
                        System.debug('List to Update is:###' +listToUpdate);
                    }
                    if(listToUpdate.size() > 0)
                    {
                        update(listToUpdate); 
                    }

                  //Condition for obj2 update
                  //......

                    List < obj1 > listToUpdate = new List<obj1>();
                    for( Obj1  o1: (List<obj1>)scope)
                    { 
                        if (o1.Test__c == true){
                            o1.Test__c = false;
                        }else {
                                o1.Test__c = true;
                              }

                        listToUpdate.add(o1);
                        System.debug('List to Update is:###' +listToUpdate);
                    }
                    if(listToUpdate.size() > 0)
                    {
                        update(listToUpdate); 
                    }

                }



            public void finish(Database.BatchableContext BC) {

                   if (currentBatchChain == 1) {
                System.debug('In First Chain#######');
                Batch_actualizadorPagos mb = new Batch_actualizadorPagos(currentBatchChain + 1);
                Database.executeBatch(mb, 5000);
                 }
                     else if (currentBatchChain == 2) {
                        System.debug('In 2nd Chain#######');
                        Batch_act mb2 = new Batch_act(currentBatchChain + 1);
                        Database.executeBatch(mb2, 5000);   
                }
                else if (currentBatchChain == 3) {
                    System.debug('In 3rd Chain#######');
                    Batch_act mb3 = new Batch_act(currentBatchChain + 1);
                    Database.executeBatch(mb3, 5000);

                }
         }
}
1
  • You can use SOSL but it has limit of 2k records. Maybe have a batch which accepts some parameter in constructor, based on which it updates object 1 or two
    – Raul
    Feb 10 '17 at 12:36
1

You need to update your execute method in a following way:

public void execute(Database.BatchableContext BC, List < sObject > scope) {
    if ( currentBatchChain == 1 )  {
                List < obj1 > listToUpdate = new List<obj1>();
                for( Obj1  o1: (List<obj1>)scope)
                { 
                    if (o1.Test__c == true){
                        o1.Test__c = false;
                    }else {
                            o1.Test__c = true;
                          }

                    listToUpdate.add(o1);
                    System.debug('List to Update is:###' +listToUpdate);
                }
                if(listToUpdate.size() > 0)
                {
                    update(listToUpdate); 
                }
   }
              //Condition for obj2 update
              //......
   if ( currentBatchChain == 2 ) {
                List < obj2 > listToUpdate = new List<obj2>();
                for( Obj2  o1: (List<obj2>)scope)
                { 
                    if (o1.Test__c == true){
                        o1.Test__c = false;
                    }else {
                            o1.Test__c = true;
                          }

                    listToUpdate.add(o1);
                    System.debug('List to Update is:###' +listToUpdate);
                }
                if(listToUpdate.size() > 0)
                {
                    update(listToUpdate); 
                }
   }
            }

I hope this is clear

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.