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

Implementing Scheduled Apex
I'm working on a very simple scheduled Apex case. Once a month I want to scan opportunities, and reassign ones that don't have a status date back to an early stage (this keeps our pipeline clean).
I have read a lot of forum posts and blogs on this and here is what I have so far - it's basically not executing at all (0% coverage). I'm not an Apex/Java expert so I might be missing something very obvious. Please help if you can.
Here is my implements Scheduler class:
global class OpportunityRestageScheduler implements Schedulable { global void execute(SchedulableContext ctx) { OpportunityRestage.changeStage(); } }
Here is my regular class that reassigns a stage:
global class OpportunityRestage { @future(callout=true) public static void changeStage() { List<Opportunity> opps = new List<Opportunity>(); for(Opportunity OppMaster:[select ID, StageName from Opportunity where Current_Status_Date__c = null]) { // Reassign the stage OppMaster.StageName = 'B = Target Account'; opps.add(OppMaster); } update opps; } }
Here is my test class to run the above:
@istest class OppRestageTest { public static testMethod void testOppRestaging() { Test.StartTest(); Opportunity opp1 = new Opportunity(StageName = '6 = Selection Vendor / Solution', Name = 'testmethod', CloseDate = date.today()); insert opp1; OpportunityRestageScheduler reassign = new OpportunityRestageScheduler(); Test.StopTest(); System.debug('Stage update'); System.debug(opp1.StageName); } }
I based the code off this Salesforce blog post which implements the Apex scheduler: http://blog.sforce.com/sforce/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html
Thanks. This helped me solve the issue. Instead of calling the Scheduled class from my test script, I just called the actual class that did the change like you suggested below. Then, I scheduled the Scheduler class within SF and this all appears to work. The opportunities are getting reassigned.
All Answers
I think it's becuase you create the Opportunity within the test.starttest() construct.
One question - are you sure you don't need to process the updates via a Batch? I know the @future gives you some higher limits, but if you think you'll have more than a few hundred rows to update, you should consider changing the reassign process to be a batch apex process.
For the problem at hand though, I would also split the tests so that you have one for the Scheduler, and one for the Re-Assignment Code:
And for the Scheduler:
Thanks. This helped me solve the issue. Instead of calling the Scheduled class from my test script, I just called the actual class that did the change like you suggested below. Then, I scheduled the Scheduler class within SF and this all appears to work. The opportunities are getting reassigned.