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
RiojinRiojin 

Hi i need help with code coverage in this class!

I am a newbie and I find the code coverage very difficult, if anyone can help me, would be grateful, 

 

This is the class.

 

 

global class ScheduledProducts implements Schedulable {
global void execute(SchedulableContext SC){

Datetime fivedays = DateTime.now().addDays(-5);

List<Products__c> lastDays=
[SELECT Is_Approved__c, CreatedDate FROM Products__c
WHERE CreatedDate <: fivedays AND Is_Approved__c = false];

for(Products__c a : lastDays){
if(a.Is_Approved__c == false) {
a.Is_Approved__c = true;
}
       } 
   }
}

 

Thanks in advance!

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

@istest

class ScheduleTestClassProduct {

 

   static testmethod void test()

    {

           Test.startTest();

   Products__c Pro = new Products__c(Name='testName',Is_Approved__c=false);

 

        insert Pro;

  

       String dateTime113 = '0 0 0 3 9 ? 2022';

      String jobId = System.schedule('testBasicScheduledApex',dateTime113 , new ScheduledProducts());

     

      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered , NextFireTime  FROM CronTrigger WHERE id = :jobId];

  Test.stopTest();

   }

}

 

For more detail follow below link:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

RiojinRiojin

Hi, thanks for you reply.

 

Eclipse show a error : Field is not writeable: Products__c.Name 

 

Products__c Pro = new Products__c(Name='testName',Is_Approved__c=false);

 

 

 

Thanks!

RiojinRiojin

I think the test should check the products that were created for five days or more, and these are approved after the scheduled run.