+ Start a Discussion
SayasoniSayasoni 

Help with my Apex Scheduler

I need to send a notification to a user for deals that Annula Review date(custom filed) equals to Today's date.

I have an apex scheduler class and the class that does the SingleEmail messaging.

My problem is that nothing seems to work since my query for retrieving the records doesn't seem to return any records despite having several records meet that criteria.I have even tried querying using a diffrent field value not the date and this too doesn't seem to return any results.Kindly assist.

Below is my controller for sending the email as well as the Apex scheduler.

 

public class SendAnnualReviewNote {

  public void sendNotification() {
	Date d = Date.today();					
	List<Deals__c> deals = [Select Id,Deal_Start_Date__c,Annual_Review_Formula_Date__c,Name,Contact__c from Deals__c where Annual_review_date__c = :d];
   for(Deals__c deal : dealz) {
	   Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
		String [] toAddresses = New String[]{'xxx@xxx.com'};
		email.setSubject('Deals Due For Annual Review');
		email.setPlainTextBody('The following deals are due for Annual Review in the next one month: ' +dealz[0].Name+ '.');
		email.setToAddresses(toAddresses);
		
		//Send the email
		Messaging.sendEmail(New Messaging.Singleemailmessage[]{email});	
	
	}
   }
	

}

 Scheduler:

global class ScheduleAnnualReview implements Schedulable{
	
	global void execute(SchedulableContext ctx) {
	  SendAnnualReviewNote sc =  new SendAnnualReviewNote();
	  sc.sendNotification();		
	}	

	public static testMethod void testScheduler() {
	  Test.startTest();
		ScheduleAnnualReview sch =  new ScheduleAnnualReview();
		String schd = '0 0 23 * * ?';	
                System.schedule('Test Annual Review', schd, sch);
           Test.stopTest();
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

You have to schedule your scheduler class to any specific time from going to

Apex Classes--> Click on Schedule Apex button--> Give "Job Name" and select "ScheduleAnnualReview" from "Apex Class" list. Now set your time for scheduling according to your need.

 

The other issue in your query

 

"Select Id,Deal_Start_Date__c,Annual_Review_Formula_Date__c,Name,Contact__c from Deals__c where Annual_review_date__c = : d" is, you are retreiving "Annual_Review_Formula_Date__c" while in where clause you are using "Annual_review_date__c". Make sure to use same fields in query.

 

Please check for any exception by debugging using "system.debug()".

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

You have to schedule your scheduler class to any specific time from going to

Apex Classes--> Click on Schedule Apex button--> Give "Job Name" and select "ScheduleAnnualReview" from "Apex Class" list. Now set your time for scheduling according to your need.

 

The other issue in your query

 

"Select Id,Deal_Start_Date__c,Annual_Review_Formula_Date__c,Name,Contact__c from Deals__c where Annual_review_date__c = : d" is, you are retreiving "Annual_Review_Formula_Date__c" while in where clause you are using "Annual_review_date__c". Make sure to use same fields in query.

 

Please check for any exception by debugging using "system.debug()".

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
SayasoniSayasoni

Thanks alot Ankit,i didn't know about the Schedule Apex button,that was really helpful.The email was sent successfully.However i would like to set the time in my code as i have seen in the Apex scheduler documentation,problem is i didn't find the documentation to be clear enough on where exactly i should be able to set the time e.g i would like my class to be run everyday at 9 AM i.e String = '0 30 9 * * * ?' 

I need this due to the limitation of only being able to schedule the apex only on hourly basis if i do it using the Schedule Apex button. 

SayasoniSayasoni

I found a way of doing it through the code by executing the following method in the developer console:

ScheduleAnnualReview m = new ScheduleAnnualReview();
String s = '0 30 8 * * ?';
system.schedule('Annual Review', s, m);

 

Thus,my class will run everyday at 8.30AM.