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
JD2010JD2010 

Batch Apex Best Practice/Info

Currently examining an update that we want to make to our system where daily, our system checks the date of particular items, and if they're past the current date, an action is ran. I've been looking into batch apex and some other features of SF trying to determine what may be the best route for this, and was curious as to whether batch apex would be the best route for this? Can anyone shine some possible light on what the best instances are to use batch apex?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
dev401hasdev401has

Looking at your requirement I would recommened Apex Scheduler. You need to schedule the class everyday which will check the dates everyday for your items and fetch you the records which are past todays date and then you can process on it.

If the records fetched everyday are more than 10000 then you can go for batch apex wich will be called from Apex scheduler class.

 

I hope I am clear with my words

All Answers

dev401hasdev401has

Looking at your requirement I would recommened Apex Scheduler. You need to schedule the class everyday which will check the dates everyday for your items and fetch you the records which are past todays date and then you can process on it.

If the records fetched everyday are more than 10000 then you can go for batch apex wich will be called from Apex scheduler class.

 

I hope I am clear with my words

This was selected as the best answer
Ankit AroraAnkit Arora

As you want to check the date on daily basis, then you can implement this using a Schedule class. This schedule class will be having a simple apex logic and will be scheduled for daily.

 

But the records on which you are working are in bulk (more than 50K) then the only option to go with is batch. You can also use schedule class to schedule this batch for daily.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

DevGarethmdDevGarethmd

Depending on your use case time dependent workflow could be an alternative to the Apex Scheduler suggested in other posts. It will mean that workflow actions are effectively queued pending some future date condition rather than you have to run some routine on a daily basis. If what you want to is quite simple like update a field on your object then it could be a good approach for you.

Ankit AroraAnkit Arora

Workflow is triggered once the record is updated or inserted. Lets say I have written a workflow which will send an email alert if the date field is in past.

 

I have inserted two records one with date 1 month back and one with one month in future. As soon as I insert both records email will be sent regarding the record which is having date 1 month ago. Please note that no action will be queued up for the second record as it will not meet the criteria till the date is in past.

 

Now an email will never be sent till the record is updated (so it will meet the criteria and action can be triggered). So we have suggested schedule which will check the status of date field on daily basis. We can do this task using combination of workflow and schedule, but not only with workflow.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

JD2010JD2010

Ankit brings up a good point with the workflow. The other issue, too, is that field updates via the workflow do not kick-off additional workflow, which may need to be possible. Not a bad idea though as far as thinking about future stuff. Though it could inherently work if you set the time-based workflo to, at X date, update field Y to a particular status.

 

The scheduled and batch answers that were given worked out. I just wrote a scheduled class that runs a batch job to ensure that we don't hit our governor limits (or that if we do, it's handled properly). Tested it out in our dev org and it worked like a charm!

 

Appreciate the feedback, all!