1

I'm having trouble creating a BATC apex. I need to make a condition in the start method

If the record type of opportunity equals donation executes a query if the record type = membership runs another query

global Database.QueryLocator start(Database.BatchableContext BC){
   ;  

// if (r.DeveloperName.equals('Donation')){
 /*   return Database.getQueryLocator([
                SELECT Id, (...) Where Amount__c > 50
           else {
           */
            return Database.getQueryLocator([
                SELECT  Name, (....) Where Amount__c < 50

           ]); 

But that doesn't result. Can anyone help?

1
  • Without querying the record its difficult to find record type so couple of questions 1)Which object you have main query 2)How is your class invoked from scheduler or a visualforce page ? Aug 9, 2013 at 10:30

2 Answers 2

2

Use this method like this:

String batchQuery;  

if (r.DeveloperName.equals('Donation'))
{
    batchQuery = 'SELECT id FROM... Amount < 50';
}
else 
{
     batchQuery = 'SELECT id FROM... Amount > 50';
}

return Database.getQueryLocator(batchQuery);
1

I always generate my queries for batches outside the batch - makes it easier to set, especially for testing...

So you have a batch class like this:

global class myBatchClass implements Database.Batchable<sObject> {

public String query;

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

And then I invoke it like this:

myBatchClass thisBatch = new myBatchClass();

String batchQuery;

if (r.DeveloperName.equals('Donation') ) {
    batchQuery = 'SELECT id FROM... Amount < 50';
}
else  {
    batchQuery = 'SELECT id FROM... Amount > 50';
}

kwBatch.query = batchQuery;
database.executebatch(thisBatch);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .