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
DEV_CGDEV_CG 

how to write 2 queries in the start method of batch class based on condition

select id,name,email from Account where createddate=today
select accountid,Account.name from Contact where account.closeddate=today and createdate=today
Best Answer chosen by DEV_CG
Raj VakatiRaj Vakati
They only way you can do it based on the contructor ..


Crate a two differnet construtcors and first one for one query and second one for seconf query as show below
 
global class DemoBatch implements Database.Batchable<sObject>{

   global  String Query;
   

   global DemoBatch(String q1){

      Query=q1;
   }
   
   global DemoBatch(){

      Query='select accountid,Account.name from Contact where account.closeddate=today and createdate=today'
   }


   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     for(sobject s : scope){
     s.put(Field,Value); 
     }
     update scope;
    }

   global void finish(Database.BatchableContext BC){
   }
}


And You can excute it as 
 
// Second Query batch
Id batchInstanceId = Database.executeBatch(new DemoBatch(), 5);

// First Query batch
Id batchInstanceId1= Database.executeBatch(new DemoBatch('select id,name,email from Account where createddate=today'), 5);

 

All Answers

Raj VakatiRaj Vakati
They only way you can do it based on the contructor ..


Crate a two differnet construtcors and first one for one query and second one for seconf query as show below
 
global class DemoBatch implements Database.Batchable<sObject>{

   global  String Query;
   

   global DemoBatch(String q1){

      Query=q1;
   }
   
   global DemoBatch(){

      Query='select accountid,Account.name from Contact where account.closeddate=today and createdate=today'
   }


   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     for(sobject s : scope){
     s.put(Field,Value); 
     }
     update scope;
    }

   global void finish(Database.BatchableContext BC){
   }
}


And You can excute it as 
 
// Second Query batch
Id batchInstanceId = Database.executeBatch(new DemoBatch(), 5);

// First Query batch
Id batchInstanceId1= Database.executeBatch(new DemoBatch('select id,name,email from Account where createddate=today'), 5);

 
This was selected as the best answer
DEV_CGDEV_CG
hi Raj,
thanks for quick reply..I tried no errors ,but its not giving me proper output...i dont know where am missing
Raj VakatiRaj Vakati
I gave an though .. that how you can able to do it .. what is your requreiement ?
Raj VakatiRaj Vakati
give me u r code 
DEV_CGDEV_CG
raj, could you help me to set debug/check points,so that i will analyze where am missing...then also i wont achieve i will ping the code
Raj VakatiRaj Vakati
You need to set by System.debug 

refer ths links

https://help.salesforce.com/articleView?id=code_dev_console_checkpoints_setting.htm&type=5
DEV_CGDEV_CG
Raj thanks for your help,it seems it will work for me...
but what if have differnt API names of our 2 queries..as we need to work with filed names here...
​​​​​​​any clue?
Raj VakatiRaj Vakati
Yes ..include all fields in both the SOQL queries so that you wnt get any Exceptions 
DEV_CGDEV_CG
in execute method i just manually mentioned fileds of first query into String list  and worked on it.
so it is working for only query for me..
is it possible to get the api names of the Scope object of execute method dynamically?
so that based on condtition or based constructor it will pass those records to execute method, i use those api fileds dynamically?
Raj VakatiRaj Vakati
You can pass those values as an arguments to yye constructor
DEV_CGDEV_CG
Raj,
I dint get what exactly you are saying...include all fileds in both the soql queries means?.....
I just want to implement it to scope object fileds that will come ...
both condtions we will get 2  different set of object records(2 scopes but synchronusly)...i want to work on those 2 scope object filed names...
any clues? correct me if my assumption is wrong
DEV_CGDEV_CG
Raj,
As you mentioned above,

// Second Query batch Id batchInstanceId = Database.executeBatch(new DemoBatch(), 5); // First Query batch Id batchInstanceId1= Database.executeBatch(new DemoBatch('select id,name,email from Account where createddate=today'), 5);

if i execute these 2 lines,
 what would be the value of the Query...
what would be the scope records...are they related to query1/query2 

could you kindly explain me
Raj VakatiRaj Vakati
See My comments on

what would be the value of the Query...

The query will be based on how you are calling the batch by using the constructor 
Id batchInstanceId = Database.executeBatch(new DemoBatch(), 5)===>> Query will be 

Query='select accountid,Account.name from Contact where account.closeddate=today and createdate=today'

what would be the scope records...are they related to query1/query2 
batchInstanceId1= Database.executeBatch(new DemoBatch('select id,name,email from Account where createddate=today'), 5)===> Query will be 
'select id,name,email from Account where createddate=today')

Your scope will be the list of account  which you query will be return and you are seeing the values to 5