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
venkateshvenkatesh 

Schedule an Apex code to run in some time interval??

Hi,

   Is it possible to schedule my application built on Force.com so that my application runs in some time interval.

Can anyone gimme some link to this particular problem?? cheers

Best Answer chosen by Admin (Salesforce Developers) 
sanjumehsanjumeh
Following is simple way to schedule a task/APEX code to run every 1 hr or every ‘X’ days. Step1: Create a custom object “Batch Script”                Create a custom checkbox “Test Script Run” in the “Batch Script” Step 2: Create a trigger on “Batch Script” which will run “after update”trigger TestScript on Batch_Script__c (after update) {  for (Batch_Script__c bs : Trigger.new) {      //run whatever APEX code you want       Batch_Script__c ourBS = new Batch_Script__c (        Name='bstest',        Test_Script_Run__c=false              );      insert ourBS;  }}  This trigger creates a new “Batch Script” object everytime you save a “Batch Script” object.  

Step3: Create a time based workflow which will run 1hr after the object instance has been created and update the checkbox field.

 

Consequently after 1 hr of the object instance creation the workflow will update the record’s field. Instantly the trigger will now run which will create a new object instance and run your extra logic in APEX as well. This will again run the workflow (as a new record has been created) and after 1 hr the new record’s field  will get updated thereby causing the trigger to run again. So there is a cycle with 1 hr gap.

 

I got this from here: http://gokubi.com/archives/daily-cron-jobs-with-apex

 

You will typically find a few minutes lag in the 1 hr interval. 

 

- Sanjeev

All Answers

bikla78bikla78

I am also facing a similar issue.. I want to execute a field update based when a event.date is less that today's date.  The only way I can really do this is to have my APEX class run at a scheuled time since it's not relaly a before insert, update etc.....

 

i wonder if there is a way we can schedule a class and /or trigger

Message Edited by bikla78 on 02-11-2009 10:16 PM
JimRaeJimRae
Look for CronKit on the AppExchange.  It will allow you to execute Apex Code on a defined interval.
venkateshvenkatesh
but dnt you think i have to pay em :(  cant i generate my own apex code for batch scheduling :)
JimRaeJimRae
It is a free application.
bikla78bikla78
Thanks Jim I willl look into the cron kit.
venkateshvenkatesh

Jim,

 Thanks for that info , i am looking forward to this CronKit.

So is it the onlyway to use some other products like CronKit available in appexchange or there is some native methods in Apex that we can use for scheduling.

sanjumehsanjumeh
Following is simple way to schedule a task/APEX code to run every 1 hr or every ‘X’ days. Step1: Create a custom object “Batch Script”                Create a custom checkbox “Test Script Run” in the “Batch Script” Step 2: Create a trigger on “Batch Script” which will run “after update”trigger TestScript on Batch_Script__c (after update) {  for (Batch_Script__c bs : Trigger.new) {      //run whatever APEX code you want       Batch_Script__c ourBS = new Batch_Script__c (        Name='bstest',        Test_Script_Run__c=false              );      insert ourBS;  }}  This trigger creates a new “Batch Script” object everytime you save a “Batch Script” object.  

Step3: Create a time based workflow which will run 1hr after the object instance has been created and update the checkbox field.

 

Consequently after 1 hr of the object instance creation the workflow will update the record’s field. Instantly the trigger will now run which will create a new object instance and run your extra logic in APEX as well. This will again run the workflow (as a new record has been created) and after 1 hr the new record’s field  will get updated thereby causing the trigger to run again. So there is a cycle with 1 hr gap.

 

I got this from here: http://gokubi.com/archives/daily-cron-jobs-with-apex

 

You will typically find a few minutes lag in the 1 hr interval. 

 

- Sanjeev

This was selected as the best answer
JimRaeJimRae

The suggestion that sanjumeh is exactly what cronkit does.  It just creates the workflow and execution trigger for you.  The time based workflow is the way to execute things on an interval basis. Your other option is to go outside the product, use a real cron type scheduling tool and execute a web service or other api style call.

 

venkateshvenkatesh

sanjumeh that really helps to solve my problem with some modifications ..I just need to take a look at what more i can do within my limitations..great articles out there..cheers

Raghu_devRaghu_dev
Thank you. That really helped.