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
Irish@accIrish@acc 

Need help!!Test class for schedular class

global class CreateRootCause implements Database.Batchable<sObject>,
Schedulable {

public String Query;
global CreateRootCause(){

String ProjectQuery = 'Select Id,Name,Dev_Lead_User__c from Project__c where Overall_Project_Status__c NOT IN (\'Not Started\',\'On hold\') AND Project_TQL_Phase__c !=\'1. Kick-off\' AND Project_State__c IN(\'Active\')';

        
        
        this.Query=ProjectQuery;         
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope)
    {
        List<Project__c> ProjectList =(Project__c[]) scope; 
                               
    
    for(project__c prj: ProjectList)
    { 

        List<Task__c> Lsttask=new List<Task__c>();
        Lsttask=[Select id,project__c from Task__c where project__c=: prj.Id];  
        List<Root_Cause__c> lstRC=new List<Root_Cause__c>();
    for(Task__c thistask: Lsttask)
        {
            
            Root_Cause__c RCDel= new Root_Cause__c();
            RCDel.TaskName__c=thistask.Id;
            RCDel.Date__c=System.Today();  
            RCDel.Final__c=true;
            RCDel.Project_Name__c=thistask.Project__c;
            RCDel.Launch_Date_Delay__c=0; 
            RCDel.Dev_Lead__c=prj.Dev_Lead_User__c;     
            lstRC.add(RCDel);     
            //lsttask.add(thistask);
        }    
  
     insert lstRC;
    }
 
}
global void finish(Database.BatchableContext BC){
}
  

                //Execute batch class with default constructor from schedular
    global void execute(SchedulableContext sc) {
        
        
       try {
    
         database.executeBatch(new CreateRootCause());

        }
        catch (Exception e) {
            System.debug('There are no jobs currently scheduled. ' + e.getMessage()); 
        } 
   }   
    
}

How to write test class for the above code..please help me on this...

Best Answer chosen by Admin (Salesforce Developers) 
Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Irish

 

Use following code for your batch:

 

public class CreateRootCauseTest{
          static testMethod void testCreateRootCause(){
                   Project__c proj = new Project__c();
                   proj.Name = 'Test Proj';
                   proj.Overall_Project_Status__c = 'Test';
                   proj.Project_TQL_Phase__c = '2.Kick';
                   proj.Project_State__c = 'Active';
                   insert proj;

                   Test.startTest();
                           CreateRootCause crc = new CreateRootCause();
                            Database.executeBatch(crc, 1);
                   Test.stopTest();
          }
}

 

If this is you solution then please mark as a solution and it helps other for similar issues.

All Answers

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Irish

 

Use following code for your batch:

 

public class CreateRootCauseTest{
          static testMethod void testCreateRootCause(){
                   Project__c proj = new Project__c();
                   proj.Name = 'Test Proj';
                   proj.Overall_Project_Status__c = 'Test';
                   proj.Project_TQL_Phase__c = '2.Kick';
                   proj.Project_State__c = 'Active';
                   insert proj;

                   Test.startTest();
                           CreateRootCause crc = new CreateRootCause();
                            Database.executeBatch(crc, 1);
                   Test.stopTest();
          }
}

 

If this is you solution then please mark as a solution and it helps other for similar issues.

This was selected as the best answer
Irish@accIrish@acc
Thanks for the quick response
Irish@accIrish@acc
Hi Singh,

can you please tell me how to setup date for Roll summary field???
Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Irish,

 

Yes, it is still not possible to use TODAY() or NOW() functions when filtering for Roll Up Summaries.

 

But you can create a formula Date type field say Date__c. Put all loging for required date into formula field and then you can use this field Date__c for roll summary field.