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 ? – Mohith Shrivastava Aug 9 '13 at 10:30
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.