function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Surender reddy SalukutiSurender reddy Salukuti 

Error in Batch Apex

when i written the below  program using batch apex topic its getting error in line 1 and 6
global class Batch_Example implements Database.Batchable<sobject>{
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query='Select id,name.stagename,CloseDate from opportunity Where Createddate=THIS_MONTH';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<opportunity> optList){
        for(opportunity op:optList)
        {
            op.StageName='closed Won';
            op.Closedate=system.today();
        }
        update optList;
    }
    global void finish(Database.BatchableContext bc){
        
    }

}

error message -1.Class Batch_Example must implement the method: void Database.Batchable<SObject>.execute(Database.BatchableContext, List<SObject>) 
2.global methods do not support parameter type of List<Opportunity>
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Surender,

The problem is the implementation of execute method. It has wrong parameters. Check out this code:
 
global void execute(Database.BatchableContext bc,List<opportunity> optList){
}

// Should be

global void execute(Database.BatchableContext BC, List<sObject> scope) {
   List<Opportunity> optList = (List<Opportunity>) scope;
   // do your things here
}


Regards.
Deepali KulshresthaDeepali Kulshrestha
Hi Surender,

I tried the same code but I make only make a change in the query and it works fine.

String query='Select id,name,stagename,CloseDate from opportunity WHERE CreatedDate = THIS_MONTH';

In your code in your query you have written "name.stagename" that is incorrect.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

 
Surender reddy SalukutiSurender reddy Salukuti
Thank you for your response again it's showing same error it's not working.
Adilson Arcoverde JrAdilson Arcoverde Jr
Surender,

Which API version are you using?

I used API 41.0 and worked without any errors.

Regards.