Leveraging Scheduled Apex Classes for Automation in Salesforce

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

Hello friends, we will discuss Leveraging Scheduled Apex Classes for Automation in Salesforce. Further, we will discuss how to schedule a class that runs daily.

A Scheduled Apex class in Salesforce is a custom Apex class that can be scheduled to run at specific times or intervals. It allows developers to automate repetitive tasks or processes that need to be executed at regular intervals, such as daily, weekly, or monthly. This feature is particularly useful for tasks like data clean-up, batch processing, and routine reporting.

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.
  3. Automation: Automate repetitive tasks, reducing the need for manual intervention.
  4. Efficiency: Scheduled processes can run during off-peak hours, optimizing system performance.
  5. Consistency: Ensure that critical tasks are executed consistently and on time, without relying on user action.
  6. Scalability: Handle large data volumes or complex processes that need to be executed in batches.

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
What’s your Reaction?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

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

* By using this form you agree with the storage and handling of your data by this website.