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
MaheemSamMaheemSam 

Schedule apex class to run every 1 hour

Hi,

  I want to schedule belows class to run every 1hour I tried using the scheduler to run every day at a specific time but this is not working please suggest me how to make this work. 
 
list<String> toAddresses = new List<String>();
 list<Messaging.SingleEmailMessage> mailsToSend = new List<Messaging.SingleEmailMessage>();
 list<opportunity> opp = new list<opportunity>();
 String htmlBody; 

 opp = [select id,name,account.name,owner.name,owner.email, 
               Stage_Prior_Value__c,StageName,Close_Date_Prior_Value__c,
               closedate,f_ACV__c 
         from Opportunity 
         where Stage_Prior_Value__c = '2 - Contracts' or Stage_Prior_Value__c = '3rd Notice - Contracts' ];

for(Opportunity Op: Opp){
 htmlbody = '<html><body>';             
 htmlbody += '<p><strong> Owner Name : ' + op.owner.name + '</strong><br/></p>';
 htmlbody += '<p><strong> Account Name : ' + op.Account.name + '</strong><br/></p>';    
 htmlbody += '<p><strong> ACV Amount : ' + op.f_ACV__c + '</strong><br/></p>';    
 htmlbody += '<p><strong> Prior Forecast Category : ' + op.Stage_Prior_Value__c + '</strong><br/></p>';    
 htmlbody += '<p><strong> New Forecast Category : ' + op.stagename + '</strong><br/></p>';       
 htmlbody += '</body> </html>';   
          
      toAddresses.add('sudhir.narayanaswamy@gmail.com')    
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setToAddresses(toAddresses);      
      mail.setSenderDisplayName('Deals moving out of Commit forecast ');
      mail.setSubject('Deals moving out of Commit forecast ');
      mail.setUseSignature(false);            
      mail.setHtmlBody(htmlBody);
      mailsToSend.add(mail);
      Messaging.sendEmail(mailsToSend);    
    
}

Thanks
Sudhir
Daniel B ProbertDaniel B Probert
Hi Sudhir,

You need to do this from developer console, this stackexchange post covers it and i used this to acheive the same:

https://salesforce.stackexchange.com/questions/16337/schedule-hourly-batch-using-cron-expression-start-on-abritrary-day-of-month

thanks
dan
shubhi vijayvergiashubhi vijayvergia
Hi Sudhir,

To schedule above class you need to implement Schedulable interface.
Below is sample code:-
global class scheduledHourly implements Schedulable {
	global void execute(SchedulableContext SC) {
		list<String> toAddresses = new List<String>();
		list<Messaging.SingleEmailMessage> mailsToSend = new List<Messaging.SingleEmailMessage>();
		list<opportunity> opp = new list<opportunity>();
		String htmlBody; 

		opp = [select id,name,account.name,owner.name,owner.email, 
		Stage_Prior_Value__c,StageName,Close_Date_Prior_Value__c,
		closedate,f_ACV__c 
		from Opportunity 
		where Stage_Prior_Value__c = '2 - Contracts' or Stage_Prior_Value__c = '3rd Notice - Contracts' ];

		for(Opportunity Op: Opp){
			htmlbody = '<html><body>';             
			htmlbody += '<p><strong> Owner Name : ' + op.owner.name + '</strong><br/></p>';
			htmlbody += '<p><strong> Account Name : ' + op.Account.name + '</strong><br/></p>';    
			htmlbody += '<p><strong> ACV Amount : ' + op.f_ACV__c + '</strong><br/></p>';    
			htmlbody += '<p><strong> Prior Forecast Category : ' + op.Stage_Prior_Value__c + '</strong><br/></p>';    
			htmlbody += '<p><strong> New Forecast Category : ' + op.stagename + '</strong><br/></p>';       
			htmlbody += '</body> </html>';   

			toAddresses.add('sudhir.narayanaswamy@gmail.com')    
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			mail.setToAddresses(toAddresses);      
			mail.setSenderDisplayName('Deals moving out of Commit forecast ');
			mail.setSubject('Deals moving out of Commit forecast ');
			mail.setUseSignature(false);            
			mail.setHtmlBody(htmlBody);
			mailsToSend.add(mail);
			Messaging.sendEmail(mailsToSend);    

		}
	}
}
After saving this scheduler class.
Schedule this apex class using "Schedule Apex" . For scheduling it hourly either you have to schedule it 24 times using "Sxchedule apex". Or you can run the below code from developer console:-
String CRON_EXP = '0 0 * * * ?'; 
scheduledHourly sch = new scheduledHourly(); 
system.schedule('Hourly Example Batch Schedule job', CRON_EXP, sch);


Please let me know if this solves your query.

Thanks
Shubhi