How to update 2 objects from batch apex

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);

                }
         }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x