Daily cron jobs with Apex

Last Updated on Tuesday, 17 April 2007 01:48 Written by Steve Tuesday, 17 April 2007 01:48

To date, there has been no way to run code that manipulates Salesforce.com on a predetermined schedule, without having an external server and some sort of scheduler. Lots of people are running these kinds of scripts, but the folks I work with don’t have those kinds of resources. One of the things I love about on-demand is that I don’t have to keep servers running, so I shied away from writing these kinds of scripts.

In playing with Apex, I came up with a way to run Apex scripts every day, with no user intervention. It relies on two things: time-based workflow and Apex triggers. Here’s a run down of how it works.

I created a custom object called Batch Script and then created on instance of that object.

batch_script.png

It’s got two fields that matter: a last run date field and a run checkbox. The next step is to create a timebased workflow on the object.

cron_field_update1.png

The workflow kicks off when the last run date is one day ago. When this state is reached, the run checkbox is updated.

Now, and Apex trigger fires on the update of the Batch Script object. Here’s the code:

trigger TestScript on Batch_Script__c bulk (after update) {
//if checkbox is checked, run the script
  for (Batch_Script__c bs : Trigger.new) {
    if (bs.Test_Script_Run__c == true) {
      //run whatever APEX code you want
      Batch_Script__c ourBS = new Batch_Script__c (
        Id = bs.Id,
        Test_Script_Run__c=false,
        Test_Script_Last_Run_Date__c=system.today()
      );
      update ourBS;

    }
  }
}

This trigger runs arbitrary Apex code if the run checkbox is checked. After running the code, the run checkbox is unchecked and the last run date is set to today. Tomorrow, this will happen all over again–the code will be run and the object will be returned to a state where it will fire again in 24 hours.

Do you have more control with a cron job running on an external server? Sure. Is this a kludge? Absolutely. But if all you want to do is run daily jobs that use Apex to work with your data, you can use this method and save the headache of outside servers.

These new pieces of functionality are making things possible that weren’t possible before. That’s a great sign for the future of the platform–as long as they make more things possible, the platform will continue to get more powerful.

Learn More

Just a normal weekend

Last Updated on Sunday, 6 December 2009 11:49 Written by Steve Sunday, 15 April 2007 12:32

DSC_0032.JPG

Learn More

Kurt Vonnegut, RIP

Last Updated on Thursday, 12 April 2007 07:25 Written by Steve Thursday, 12 April 2007 07:25

A great American author passed away last night–Kurt Vonnegut died at the age of 84. If you haven’t read any of his work, you’ve missed out on some of the most creative, funny, and touching stories of the last century. Cat’s Cradle blows me away every time I think about it. He was a true genius.

In 1995 when I was looking to purchase a domain name I settled on gokubi.com after reading Vonnegut’s Galapagos, where gokubi was his name for a gadget from the future. It was an instant translator, allowing people of different languages to communicate. For no real reason other than his creativity grabbed me, I grabbed the domain name.

Take this opportunity to read some Vonnegut: Cat’s Cradle, Slaughterhouse Five, and Breakfast of Champions are all wonderful.

The most fitting Vonnegut quote for today is clearly this,

And if I should ever die, God forbid, I hope you will say, “Kurt is up in heaven now.” That’s my favorite joke.

Learn More
Copyright © 2009 Afterburner - Free GPL Template. All Rights Reserved.
WordPress is Free Software released under the GNU/GPL License.