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
Balasubramani DhanapalBalasubramani Dhanapal 

update all the opportunities close date as today using batch class ?

Hi all,
              I am begineer of salesforce.please help me the below feature development.  
Write batch to update all the opportunities close date as today() ? 

Thanks in advance 
Bala
Best Answer chosen by Balasubramani Dhanapal
Waqar Hussain SFWaqar Hussain SF
Hello.
use this code. it must solve your problem
global class updateAllOpptoday implements Database.Batchable<SObject>, Database.Stateful{
			 
    
    List<Opportunity> listRecords = new List<Opportunity>();
   
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, CloseDate From Opportunity limit 1000';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<SObject> scope){
    
        for(Opportunity obj : (Opportunity[]) scope){
            
            if(obj.CloseDate!= date.teday()){
            	obj.CloseDate= date.today();
				listRecords.add(obj);
            }
         }
    
    }
    
    global void finish(Database.BatchableContext BC){
        system.debug('list to be deleted size  :: '+listRecords.size());
        if(!listRecords.isEmpty())
            {
              update listRecords;
            }
    }
}
 also define a Schedular for batch class to run your batch class every hour.
Here is the class
global with sharing class SchedularForBatchApex2 implements Schedulable {
        
        global void execute(SchedulableContext sc) {
            
              updateAllOpptoday d = new updateAllOpptoday(); 
              database.executebatch(d, 10);
        }
        
         Public static void SchedulerMethod(){
             SchedularForBatchApex2 s = new SchedularForBatchApex2();
             string con_exp= '0 0 * * * ?';
             System.schedule('updateAllOpptoday', con_exp, s);
         }
               
 }


Please let me know if it works. 
if it works, mark it the best answere so others can take advantage from it. 

All Answers

bob_buzzardbob_buzzard
If you are a beginner, I'd suggest that you use the data loader for this instead, as that requires no coding knowledge. Simply export all open opportunities, change the close date to today and import.

As it is, you are asking for help without appearing to have tried anything yourself - that looks more like asking us to do it for you rather than provide assistance.
Waqar Hussain SFWaqar Hussain SF
Hello.
use this code. it must solve your problem
global class updateAllOpptoday implements Database.Batchable<SObject>, Database.Stateful{
			 
    
    List<Opportunity> listRecords = new List<Opportunity>();
   
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, CloseDate From Opportunity limit 1000';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<SObject> scope){
    
        for(Opportunity obj : (Opportunity[]) scope){
            
            if(obj.CloseDate!= date.teday()){
            	obj.CloseDate= date.today();
				listRecords.add(obj);
            }
         }
    
    }
    
    global void finish(Database.BatchableContext BC){
        system.debug('list to be deleted size  :: '+listRecords.size());
        if(!listRecords.isEmpty())
            {
              update listRecords;
            }
    }
}
 also define a Schedular for batch class to run your batch class every hour.
Here is the class
global with sharing class SchedularForBatchApex2 implements Schedulable {
        
        global void execute(SchedulableContext sc) {
            
              updateAllOpptoday d = new updateAllOpptoday(); 
              database.executebatch(d, 10);
        }
        
         Public static void SchedulerMethod(){
             SchedularForBatchApex2 s = new SchedularForBatchApex2();
             string con_exp= '0 0 * * * ?';
             System.schedule('updateAllOpptoday', con_exp, s);
         }
               
 }


Please let me know if it works. 
if it works, mark it the best answere so others can take advantage from it. 
This was selected as the best answer
Balasubramani DhanapalBalasubramani Dhanapal
Dear vicky,
                   Thank you so much.
Regards,
Bala