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
Satya sai sudhirSatya sai sudhir 

Hello. I want to create a scheduled classin apex on leads which will email me new leads created with their name for every hour . How do i proceed? This is my first time using it. Thank you in advance.

Best Answer chosen by Satya sai sudhir
R Z KhanR Z Khan
Hi Satya,

the class name should not be Schedulable. It should be something that explains the purpose of your class. For example EmailLeadScheduler.

but it has to implement Schedulable interface in order for you to be able to schedule it. 
For example
global class EmailLeadScheduler implements Schedulable {
    
global void execute(SchedulableContext sc){}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

}
PS: I am not a man :)

 

All Answers

R Z KhanR Z Khan

Hi Satya,

First create an Apex class that implements Schedulable. Write your logic to send the emails.
Once the class is done, go to Setup-> Scheduled Jobs adn set up the interval to be 1 hour. 

For more information check out
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
 

For informaiton on sending emails form Apex check out
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm

Let me know if oyu have any more questions

Satya sai sudhirSatya sai sudhir
@ Khan Thank you for your help sir. I have created an apex class called "Schedulable". The logic I have written in it is

global class Schedulable {
    
global void execute(SchedulableContext sc){}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

}

I have saved it . What do i do now? I went to Setup>Scheduled jobs it is empty!
R Z KhanR Z Khan
Hi Satya,

In the scheduled jobs lsit you can see all the classes that are already schedule. In order to schedule the new class go through following steps
1. Go to Setup ->Develop->Apex Classes
2. Click Schedule Apex button
3.  Select class name, name the scheduled job and specify frequency. 

 
satpracsalesforce satpracsatpracsalesforce satprac
@ Khan thank you sir. The procedure I followed is I created a class named  "Schedulable" using develper console and I saved it just as you mentioned. Now when I went to Setup ->Develop->Apex Classes and clicked on Schedule Apex button. An error is popping up stating there are no records of such type. Please see the image below. Do i need to activate any permissions? When I'm clicking on the "Lookyp" for the "ApexClass" it says "No records found of this type".
User-added image

 
R Z KhanR Z Khan
Hi Satya,

the class name should not be Schedulable. It should be something that explains the purpose of your class. For example EmailLeadScheduler.

but it has to implement Schedulable interface in order for you to be able to schedule it. 
For example
global class EmailLeadScheduler implements Schedulable {
    
global void execute(SchedulableContext sc){}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

}
PS: I am not a man :)

 
This was selected as the best answer
R Z KhanR Z Khan
Hi Satya,

Glad it works for you now. I think you chose a wrong best answer. the answer you chose is an inocrrect solution. Please pick n answer with a working solutions, so other users who have the same problem can find it easily