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
Sanjay wadge 16Sanjay wadge 16 

How do I schedule the class to run every 10 minutes and also test the class

Hi,

I have written following class which I want to -
1) run every 10 mintues
2) write a test method to get the 100% code coverage 
3) want to test this code before scheduling to make sure it works as expected.

Will appreciate your help as I am new to Apex.

global class UpdateKBArticleNoOnCase_sched implements Schedulable{

    global void execute(SchedulableContext SC) {
       
       //New Version 2.0
            
      DateTime rightNow =  DateTime.now();
      DateTime d24hAgo = rightNow.addHours(-24);
      
      // Get unique cases for which articles were added....
      
      Set<Id> setid = new Set<Id>();
    
      // for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate = YESTERDAY])
      for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate > :d24hAgo])
      { setid.add(CasesWithArticle.CaseId); }

      // for (Case cases : [Select Id, KB_Article__c from Case IN CasesWithArticle])
      for (Case cases : [Select Id, KB_Article__c from Case WHERE Id IN :setid])
      {
        
        String AllArticleNos;
        Integer i=0;
        
        List<String> ArticleNumbers = new List<String>();
        for (CaseArticle Articles : [Select KnowledgeArticle.ArticleNumber FROM CaseArticle 
             where CaseId = :cases.Id])
        { ArticleNumbers.add(Articles.KnowledgeArticle.ArticleNumber); 
          if (i == 0)
             AllArticleNos = Articles.KnowledgeArticle.ArticleNumber;
          else
             AllArticleNos += ', '+Articles.KnowledgeArticle.ArticleNumber;
          i++;
        }
        
        System.debug(cases.id + ' ' + AllArticleNos);
                 
        // Update Case
        
        cases.KB_Article__c = AllArticleNos;
        update cases;
    
       }
       
    }
}
sandeep sankhlasandeep sankhla
Hi Sanjay,

1. If you want to test batch then you can run below code from developer console and check the functionality..After executing the below code you can simply go to setup and check the schedule jobs and apex jobs and check the status there..

UpdateKBArticleNoOnCase_sched objUpdateKBArticleNoOnCase_sched = new UpdateKBArticleNoOnCase_sched();
database.executeBatch(objUpdateKBArticleNoOnCase_sched, 30); 

2. If you want to schedule this for every 10 minus then you can use below code
 Your schedule class and pass that to system.schedule method
 
 this will be your schedule class
 public with sharing class yourScheduleClassname implements Schedulable
{
    /// This method schedules the respective batch class based on given cron expression
    /**
    * Name: execute
    *
    * Parameters: SchedulableContext SC
    * Schedules the batch
    */
    public void execute(SchedulableContext SC) 
    { 
        UpdateKBArticleNoOnCase_sched batch = new UpdateKBArticleNoOnCase_sched();
        database.executeBatch(batch,30);
    }
}

Run below code to schedule it for every 10 minus

System.schedule('Scheduled Job 2', '0 10 * * * ?', new yourScheduleClassname());


3. Test coverage will be normal, you can first insert all necessary data and satisfy all condition and then you can use below code 

            UpdateKBArticleNoOnCase_sched objBatchClass = new UpdateKBArticleNoOnCase_sched();
            Test.startTest();
            Database.executeBatch(objBatchClass, 200);
            Test.stopTest();
            
            yourScheduleClassname objyourScheduleClassnameScheduler = new yourScheduleClassname();
            String sch = '0 0 23 * * ?';
           
            system.schedule('Test Territory Check', sch, objyourScheduleClassnameScheduler);


Please let me know if you need anything else on this..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

 
Sanjay wadge 16Sanjay wadge 16
Hi Sandeep,

Thanks for your kind resposne.
When I try to do first step which you mentioned regarding running the code from Developer Console, I get this error - "Argument must be an object that implements Database.Batchable".

Looks like I need to create a separate schedulable class and another class which implements Database.Batchable. Currently I have one single class which is schedulable and also has my logic built in. If I have to do two separate classes, how those classes will look like ? I tried to separate the logi part and put in another class which implements Database.Batchable. However it gave me different kind of errors and gave it up.

Any suggestion, how I can make this code work ?

-Sanjay
Irene SlessIrene Sless
Hi Sanjay
Did you find a way to get this done?