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
AngelikaAngelika 

test class for scheduled apex? Why? How?

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I think I have all the information I need. I have a made a class that implements the schedulable interface.

 

I have a system schedule method but I don't know where to put it....in the scheduable class or in a new class?

 

Here is my code...the major issue I am having right now is writting a test class. Why does Salesforce say you need one? What is the benefit of having one? Can someone jump start me with the code of Apex scheduler test class or at least tell me what I need to put in there(format)? I've read the documentation but I still don't get it.

 

global class AccountReviewScheduler implements Schedulable

{

    global void execute (SchedulableContext ctx)

    {

        sendEmail();

    }

public void sendEmail()

{

    if (Account.Next_Account_Review_Date_c == System.today().addDays(14))

    {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        Mail.setTemplateId('00XF0000000LfE1');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });

    }

}

    newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?';

 

//  I got this code from someone else. //  

system.schedule('Daily Commision Check', s, m); 

}

 

sfdcfoxsfdcfox

Why test classes?

 

Test classes provide automated testing of code that's been written. The intent of test classes is to programmatically verify that your logic works as intended. I find myself frustrated that such testing is required, because many developers will not write correctly-written tests, thus circumventing the purpose behind testing. I think that optional testing would encourage more developers to create valid test methods solely for the purpose of releasing a better product instead of going through the motions.

 

You should strive to write test methods so that your users won't run into errors while trying to use your code. Imagine if your code were not functioning, and thus the emails were not being sent out. This would be no better than where you are today.

 

Although, I'm going to stop on this subject right now, and instead ask you a better question: Why are you using Apex Code instead of a Workflow Rule? The most important thing about leveraging Apex Code is to know when to use it. Don't use Apex Code when a standard system feature will suffice.

 

All you need to do is take the following steps:

 

1) Create a new workflow rule (setup > create > workflow and approvals > workflow rules). Criteria would be Next Account Review Date greater than "next 14 days". Choose "When a record is created or updated and did not previously meet the criteria."

 

2) Add a Time-Based action for 14 days before Next Account Review Date.

 

3) Add a Workflow Alert that uses the email template you've already created.

 

4) Activate the workflow rule.

 

5) Update all of your accounts via the Apex Data Loader. This step will queue all accounts that meet the criteria from step 1 in the Workflow Action Queue.

AngelikaAngelika

I'm not using a workflow rule because I need the process to repeat every year, 14 days before the account review date. Time-based workflows don't repeat past 1 time. (This is what I had to set up initially).

sfdcfoxsfdcfox

It's still possible to set up recurring notifications with just workflows and a very small but well-placed trigger instead of going this route, but you can get away with scheduled code as well (see http://gokubi.com/archives/daily-cron-jobs-with-apex as a hypotheical example; I've built a similar function that did work).

 

If you're insisting on using scheduling, then you'll need the following pieces of information:

 

  1. System.schedule will only be called once. You will not use it in your Schedulable class at all. You will instead run it from an Execute Anonymous or a helper class, but just once.
  2. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm tells you all about the Scheduler interface, including testing it.

That's it. Your scheduler needs the three global methods (the starting point, the main loop, and the ending point), and then a small test class as discussed at the URL above, then you just schedule it using System.Schedule from the Developer Console or Eclipse's Execute Anonymous.

 

Like I said before, the "why" of using test methods doesn't really matter in your case, because it's only for those that care about releasing quality products (like the project that the team I'm on is working on currently). For a project that won't ever be sold or released to the public, testing is pretty pointless, since failing to test your code is only harming your own users and not anyone else, but salesforce.com took a heavy-handed approach and required testing coverage. I personally dislike that they took that route, even though it's sound in theory.