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
mhamberg1mhamberg1 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
mhamberg1mhamberg1

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.

 

Test.StartTest();      
       OpportunityRestage.changeStage();       
       Test.StopTest();

All Answers

BritishBoyinDCBritishBoyinDC

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:

 

 

@istest

class OppRestageTest
{
   
   public static testMethod void testOppRestaging() 
   {
       Opportunity opp1 = new Opportunity(StageName = '6 = Selection Vendor / Solution', Name = 'testmethod', CloseDate = date.today());
       insert opp1;
       
       Test.StartTest();      
       OpportunityRestage.changeStage();       
       Test.StopTest();
       
Opportunity topp1 = [Select StageName from Opportunity where Id = :opp1.Id];
       System.assertequals('B = Target Account', topp1.StageName);
   }            
}

 

 

And for the Scheduler:

 

global class OpportunityRestageScheduler implements Schedulable
{
 
    global void execute(SchedulableContext ctx)     {
       OpportunityRestage.changeStage();
    
    }    
    
public static testMethod void testschedule() {    
Test.StartTest();
OpportunityRestageScheduler sh = new OpportunityRestageScheduler();
        String sch = '0 0 23 * * ?';
        system.schedule('Test Opty Update', sch, sh);
Test.stopTest();

}
    
}

 

 

 

mhamberg1mhamberg1

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.

 

Test.StartTest();      
       OpportunityRestage.changeStage();       
       Test.StopTest();
This was selected as the best answer