You need to sign in to do that
Don't have an account?

Apex Scheduler Test
I wonder if anyone can help me with a test method for a class that implements Scheduler. It looks like this:
global class scheduledCreateOpportunity implements Schedulable{
global void execute(SchedulableContext ctx) {
//get all contracts and make a set of their ids
Map<Id, Contract> contracts = new Map<Id, Contract>([Select Id, Name, Account.PriceLevel__c, Account.Id, Related_SLC__r.Id, ContractTerm, Integrator__r.Id, Distributor__r.Id, End_User__r.Id, Representative__r.Id, EndDate
From Contract
Where EndDate =: Date.today()]);
Set<Id> sContIds = contracts.keySet();
....
}
}
It's a pretty big class, but that the end of the day the idea is that it's supposed to create opportunity from a contract at a certain time based on the contract's end date. I know how to write most of the test method, I just don't know how to call the class above correctly, and the page bellow doesn't seem to help :(
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_scheduler.htm?SearchType=Stem
I'm not using the CronTrigger, just schedualing this thing to run via front end every day at midnight. Any ideas how I might call the class for testing?
In your test method, set up your test data as usual.
Then when you're ready to call the scheduled class, you can use something like this:
test.starttest();
scheduledCreateOpportunity sco = new scheduledCreateOpportunity();
String sch = '0 0 23 * * ?';
system.schedule('Contract Creates', sch, sco);
test.stopTest();
This is testing on the assumption that the class is scheduled to run every night at 11pm (23:00)
This will execute the schedule, and you can then test that the records were updated/created as required
One thing to note - I think they recommend that the actual code you're executing is contained in a separate class, that you just create and execute in the Scheduled Class...
All Answers
In your test method, set up your test data as usual.
Then when you're ready to call the scheduled class, you can use something like this:
test.starttest();
scheduledCreateOpportunity sco = new scheduledCreateOpportunity();
String sch = '0 0 23 * * ?';
system.schedule('Contract Creates', sch, sco);
test.stopTest();
This is testing on the assumption that the class is scheduled to run every night at 11pm (23:00)
This will execute the schedule, and you can then test that the records were updated/created as required
One thing to note - I think they recommend that the actual code you're executing is contained in a separate class, that you just create and execute in the Scheduled Class...
In what you wrote, inwhere does the sh come from? Or was that supposed to be sco, what you called the new instance of the class?haha nevermind, I see it ^_^
Hmm, that definitely worked for me. Note - the test.stoptest() is what tells the test script to execute the scheduled class, so make sure that is in the right place.
As I mentioned, my scheduled class just creates a new instance of a separate class with my actual code in it including the test script), and then calls a method in that class to execute it. So might be worth trying that?
Hi,
Is there any way in SFDC by which I can call a piece of code after every X minutes(say every 2 mins)?
Please help, need urgently. I tried CronKit, but it takes at least one hour.
Thanks,
Sam