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
john miirjohn miir 

Update opportunity stage on 1st of every month

HI all please help with how to create a batch job which converts all the opportunities that are open on the 2nd of every month. 

Thanks in advance.
Best Answer chosen by john miir
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi John,
Create a apex class using Schedulable interface.

Try this code.kindly modify according to your requirements.
global class testOppClass implements Schedulable{
   
   global void execute(SchedulableContext ctx) {
       list<opportunity> oplist = [select id,name,StageName  from opportunity where StageName  = 'prospecting'];
       for(opportunity opp : oplist){
           opp.StageName  = 'closed lost'; 
       }
        update oplist;
   }
   
   
}

Then schedule this apex class to run at required time 
setup -->apex class--> click on schedule apex button-->select your schedulable class and also time to run .

If you have more records create a batch class an invoke batch from schedulable class execute method.
please refer below link whcih might help you in this
https://developer.salesforce.com/forums/?id=9062I000000IEIhQAO
https://www.appseconnect.com/apex-scheduler-in-salesforce/

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Christan G 4Christan G 4
Hi John, please elaborate more on this requirement. What would you like the opportunities that are opened on the 2nd of every month to convert to?
john miirjohn miir
Hi Christan, thank you for responding I want to convert opportunities from no response to closed lost on every 2nd 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi John,
Create a apex class using Schedulable interface.

Try this code.kindly modify according to your requirements.
global class testOppClass implements Schedulable{
   
   global void execute(SchedulableContext ctx) {
       list<opportunity> oplist = [select id,name,StageName  from opportunity where StageName  = 'prospecting'];
       for(opportunity opp : oplist){
           opp.StageName  = 'closed lost'; 
       }
        update oplist;
   }
   
   
}

Then schedule this apex class to run at required time 
setup -->apex class--> click on schedule apex button-->select your schedulable class and also time to run .

If you have more records create a batch class an invoke batch from schedulable class execute method.
please refer below link whcih might help you in this
https://developer.salesforce.com/forums/?id=9062I000000IEIhQAO
https://www.appseconnect.com/apex-scheduler-in-salesforce/

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer