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
Brad007Brad007 

Help writing a test class for Scheduler Class

Hi I am new to Apex code development.  I am really unsure about how to write test class here. Can any one please help .

Here is the scheduler class it works fine

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    
    public void SendSurvey()
    {
         // Get the most recently created config object.
         Config_Object__c config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C 
                                    where Name ='HostFamily_Survey'];
         List<Opportunity> oppList = Database.query(config.SOQL_Query__c);
         
         
             List<Account> accList = new List<Account>();             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;
                          if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c) || acc.Survey_Opp_Id__c != opp.Id)
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); 
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                           }  
                         accList.add(acc); 
                        }
                     }
                update accList;  
                          
     }
  }

 

 

But to the test class i have written the code coverage says it covered 0 lines .And how do i deply it to production. Please help. Here is the test class i have written

 

 

@isTest

private class test_Config_Survey {

 

    static testMethod void myUnitTest() {

        // TO DO: implement unit test

        //Test.startTest();

        Config_Object__c config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C 

                                    where Name ='HostFamily_Survey' limit 1];

 

        for(Opportunity oppList: [Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__c, o.Account.Name 

                                      From Opportunity o  

                                      where o.StageName='Active' 

                                      AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 

                                      AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60 limit 1])              

 

 

      Account acclist = new Account();     

      acclist = oppList.Account;

      if(acclist.Survey_Code__c !=config.HFSurveyCode__c || acclist.Survey_Opp_Id__c != '0064000000CsXem')

      {

      acclist.Survey_Code__c=config.HFSurveyCode__c ;

      acclist.Survey_Opp_Id__c = '0064000000CsXem';

      acclist.Survey_Dt__c=Date.today();

      }

      

      update acclist;  

      //Test.stopTest();    

    }

}

 

crop1645crop1645

Brad007

 

First off, take a look at the APEX Code Developer's Guide under the heading 'testing the Apex Scheduler'

 

You need to: 

* Uncomment out the Test.startTest(), Test.stopTest(). These tell the testmethod that schedule should run. Do asserts after the the Test.stopTest().

* Use the System.Schedule method in your testMethod to cause the schedulable class to be executed.

 

Best practices also suggest removing the sendSurvey() method out to a separate class that you can test independently without worrying about the scheduler logic

 

As for deployment, you deploy the class like any other APEX class.  Some nuances:

 

* If you already have the class running in PROD in a scheduled state, you'll need to delete the schedule before deploying; then reschedule

* If you need to schedule more often than weekly, then you'll need to execute anonymous APEX in the System Log with a custom CRON setting for example, daily.  Otherwise, you can use the Schedule APEX button in Setup | Develop | Apex Classes

Brad007Brad007

Hi Eric,

 

Thank yo for your response. 

so now i understand that i dont have to use global and implement schedulable interface, the other option is to write a class and implement what i need and through SFDC UI schedule the class to run for a particular timing is what your suggesting. Is it corect.

 

 

public class HF_Survey_OneMonth { 
       
  
         // Get the most recently created config object.
   List<Config_Object__c> config = [Select HFSurveyCode__c, Survey_Date__C, SOQL_Query__C from Config_Object__C 
                                   where Name ='HostFamily_Survey'];
        List<Opportunity> oppList = Database.query(config.SOQL_Query__c);                 
            List<Account> accList = new List<Account>();  
                       
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                     {
                          Account acc = opp.Account;
                         if(acc.Survey_Code__c != String.valueOf(config.HFSurveyCode__c) || acc.Survey_Opp_Id__c != opp.Id)
                           {
                             string oppcode= opp.Id;
                             acc.Survey_Code__c = String.valueOf(config.HFSurveyCode__c); 
                             acc.Survey_Dt__c = config.Survey_Date__c;
                             acc.Survey_Opp_Id__c = oppcode.subString(0,15);
                           }  
                        accList.add(acc); 
                      }
                       update accList;  
                    }
             
                          
    
  }

 

 

crop1645crop1645

Brad007

 

Sorry for the very late reply

 

1. If you want an APEX class to execute on a schedule, it needs to implement the Schedulable interface

2. If it implements the Schedualbel interface, you can start the schedule running in several ways. Using out-of-the-box SFDC , you can

  • Setup |Develop | Apex Classes | Schedule Apex
  • Invoke via the System Log - Execute Anonymous Apex something like this:
// -----------------------------------------------
// Execute anonymous code to run Foo schedulable class every day
//
//  Cron job parms are    [sec] [min] [hour] [day of month] [month] [day of week] [optional year]
//  In our case we have     0        0         12                *                     *                 ? = no val                        
// -----------------------------------------------

Foo s = new Foo();
        String sch = '0 0 12 * * ?';
        system.schedule('Foo (daily)', sch, s);
  •  You could also create a custom button that launched a custom controller action that did the same as the code above