1

I got error on below code please help me

global class OpportunityBatch implements Database.Batchable<Opportunity> {
    string Query;
    global Database.QueryLocator start(Database.BatchableContext Bc){
         Query =[select id,name from opportunity where stage = 'closed own'];
        return database.getQueryLocator (Query);

    }

    global void execute(Database.BatchableContext Bc, List<Opportunity> scope){
        for(Opportunity op : Scope){
            database.delete(op,false);
            }

    }
    global void finish(Database.BatchableContext Bc){

    }

}
5

You have some problems with code compiling.

  • Query should be enclosed with single-quote, as it is string, without "[" and "]" at the beginning and end of string, I moved query directly to Database.getQueryLocator method, as it could show error while compiling code .
  • Interface Database.Batchable should be typed Database.Batchable<SObject>

The following code is compiling

global class OpportunityBatch implements Database.Batchable<SObject> {
    global Database.QueryLocator start(Database.BatchableContext Bc){
        return Database.getQueryLocator([select id from Opportunity where StageName = 'closed own']);
    }

    global void execute(Database.BatchableContext Bc, List<Opportunity> scope){
        Database.delete(scope,false);
    }
    global void finish(Database.BatchableContext Bc){

    }
}

Do you need to do delete in execute method in loop ?

    global void execute(Database.BatchableContext Bc, List<Opportunity> scope){
        Database.delete(scope,false);
    }
|improve this answer|||||
  • Thanks Alexander ... It is working fine why we need to write query inside querylocator – kishore Jun 5 '17 at 7:11
  • Database.getQueryLocator method is overloaded method. It can accept String, or List<SObject> as parameter. If you pass String of query, you can do typos, and while compiling code you will note see any errors. If you pass List<Sobject> ([select id from Opportunity] = List<Sobject>), you will see compiling errors, if any exist in your query – Oleksandr Berehovskyi Jun 5 '17 at 7:21

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.