+ Start a Discussion
oleg.tuchschereroleg.tuchscherer 

Apex Batch working with LIST ?

Hi,

 

i have an apex batch for deleting different types of data.

So i have 3 objects (no releations to each other).

In the start method i build 3 search strings and would like to let run the execute method 3 times.

How to do that?

 

The following structure does not work:

global database.querylocator start(Database.BatchableContext bc){
        for(String query : batchHelper.buildSearchStrings())    
            return Database.getQueryLocator(query);
    }

 

Thank you for the help!

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
Create a public query variable and call the batch 3 time from console. like :
for(String query : batchHelper.buildSearchStrings()) {
BatchClass b = new BatchClass();
b.Query = query;
database.executebatch(b);
}

All Answers

Bhawani SharmaBhawani Sharma
Create a public query variable and call the batch 3 time from console. like :
for(String query : batchHelper.buildSearchStrings()) {
BatchClass b = new BatchClass();
b.Query = query;
database.executebatch(b);
}
This was selected as the best answer
oleg.tuchschereroleg.tuchscherer

Thanks !

Batch is also runnning in schedulable context. So i put your code in schedulable execute, like the following:

 

global void execute(SchedulableContext SC) {       
        for(String query : batchHelper.buildSearchStrings())
            f42_Batch_DeleteData b = new f42_Batch_DeleteData();
            b.Query = query;
            Database.executeBatch(b);    
        }
    }

 

what is to do with the START method ?

global database.querylocator start(Database.BatchableContext bc){
  
        return ???;
    }

Bhawani SharmaBhawani Sharma
This will work in normal way.


return database.getQueryLocator(database.query(query));


Regards,
Bhavi Sharma
Certified Salesforce Consultant
bhavi@simplyforce.com/bhawani.sh.sharma@gmail.com
+91-9928130589
LinkedIn |
Twitter
| Blog |
Community
www.simplyforce.com