+ Start a Discussion
ArunaAruna 

Can we write more then one soql (aggregated on  single object) in apex batch class start method --> urgent please can any one replay

Hi there ,

I am having a requirement  where I need to pass  multiple  aggregated soqls(6) on single object with different where clause  to apex batch start method and run the logic in execute method and schedule that batch class to run nightly base.

as for as I know we can pass only one soql  to start method
global Database.Querylocator start (Database.BatchableContext BC) {
                  return Database.getQueryLocator(Query);
}

But what I want is 

global Database.Querylocator start (Database.BatchableContext BC) {
                  Database.getQueryLocator(Query1);
                  Database.getQueryLocator(Query2);
                 Database.getQueryLocator(Query3);
                 Database.getQueryLocator(Query4);
                 Database.getQueryLocator(Query5);
}

You may say why don't you write single batch each query . I different queries(50) with different where clause .
If I write at least multiple queries in one batch class I can decrease number batch scheduled class.
 
PavanKPavanK
You implement only one query in start method.

i will suggest, to query all possible data in start and filter data for diffrent logic in Execute method.

Please mard answer as best if it helped anyway.
ArunaAruna
Since I am doing soql  on single object it is very difficult to identify which records are coming  :( 
PavanKPavanK
One more reason I will suggest to use single query because multiple queries may cause multiple list(To update same records) with same records. Which may cause multiple DML multiple for loops processing and as result you may run into DML operation

Here is an example, which may help for implementation
 
Start Query:

listSobj = Select FieldsRequiredforprocessing from Objectname where (Condition1) or (Condition2) or (Condition3)

Execute method:
list insertorUpdatelist;
for(Objectname objName: listSobj )
{
       if(Condition1)
       {
             objName.field= value;
      }
     if(Condition1)
     {
            objName.field= value;
      }
     if(Condition1)
     {
                objName.field= value;
     }
       insertorUpdatelist.add(objName);//collect object in list

}
if(insertorUpdatelist.size()>0)
    insert or update record
Please mark as best answer if it helped




 
ArunaAruna
Thank you Pavan .

So in that case I need to write 12 batch class. Please see my tables in this below link 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kAkQIAU

I Having  6 tables  each table is for current year and prior year .
I need to do grouping by rep, regional manager and RVP.

please look @ the table you will understand .

Thank you
Aruna.
 
PavanKPavanK
I dont think 12 batch is ideal, i will say its on how you hadnle logic in execute block.

As i said you can query data for this and prior year in start and you can group(With the help of collection variables)  them in execute method.
ArunaAruna
ok ,

but when we get year data means there will be too many rows and is it not the processing time will be by looping through all the records .
Logic implementation also will be complex right.
 and if you look @ my tables i need to do grouping based on product level in one table, group by rep one table, group by region one table, group by state one table ects .

if i write one query how I am going to do all these groupings and how do I going to get current month till date ,prior year current month till date,
total prior year current month, current year till date,prior year til date, total current year, total prior year, 4 quarters of data  ?

Thank you
Aruna.