Scheduled Apex Class in Salesforce

by Rijwan Mohmmed
3 comments
scheduled-apex-class-in-salesforce

Hello friends, we will discuss How Scheduled Apex Class in Salesforce. further, we will discuss how to schedule a class that runs daily.

Schedule Apex classes to run at specific times by implementing the Schedulable interface first, then specifying the schedule through either the Schedule Apex page in Salesforce’s user interface or the System.schedule method.

Also check this : Relationship Query in Apex Salesforce

Key Highlights :

  1. Schedulable Apex class should by implementing the Schedulable interface.
  2. Can Schedule by Salesforce directly.

Process :

Run Every 30 Minute :

ScheduledApexJob obj = new ScheduledApexJob();
 
String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);
 
String sch2 = '0 30 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

Run Daily on a particular Time: Below code schedule, a class and class will run daily 5 AM.

ScheduledApexJob obj = new ScheduledApexJob();
 
String sch1 = '0 0 5* * * ?';
System.schedule('Schedule Job1', sch1, obj);

Runs only once for a particular date-time: Below code schedule a class for after 2 and run only once.

ScheduledApexJob obj = new ScheduledApexJob();

Datetime next = System.now().addhours(2);
String sch1 = next.second() + ' ' + next.minute() + ' ' + next.hour() + ' ' +
              next.day() + ' ' + next.month() + ' ? ' + next.year();

Integer randomNumber = Integer.valueof((Math.random() * 1000000));

System.schedule('Run after 2 hours '+randomNumber, cron, obj);

You can also create crone expression by this website http://www.cronmaker.com/

Reference :

  1. Apex Scheduler

You may also like

3 comments

Pasquale November 8, 2021 - 7:41 am

Its like you read my mind! You appear to know
so much about this, like you wrote the book in it or something.

I think that you could do with a few pics to drive the message home a bit, but instead of that, this is fantastic blog.
An excellent read. I’ll certainly be back.

Reply
Deltax November 8, 2021 - 8:57 am

Thanks @Pasquale for feedback 😊

Reply
Shaunte November 10, 2021 - 3:46 am

What i don’t realize is in truth how you are not really a lot
more smartly-appreciated than you may be right now.
You’re so intelligent. You already know therefore
significantly when it comes to this subject, produced me
personally consider it from a lot of various angles. Its like men and women are not interested until it is one thing to accomplish with Lady gaga!

Your own stuffs outstanding. At all times deal with it up!

Reply

Leave a Comment