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
Ashwani PradhanAshwani Pradhan 

Test class for schedulable job with SOQL and method call

Hi,

I am not not able to write test class with more than 75% coverage. Please help me with test class for below job -

global class CreateProjectTaskBatchJob implements Schedulable{
    global void execute(SchedulableContext SC) {
        List<String> orderIds = New List<String>();
        List<Order> orders = [select Id from order where Type='Asset' and Close_Won_Date__c >= TODAY and Order_Location__c != NULL];
        for(Order o : orders) {           
            orderIds.add(String.valueOf(o.id));            
        }
        
        if(orderIds.size() > 0) {
            CreateProjectTask.createTasks(orderIds);
        }
    }
}

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashwani,

Can you please post the test class which you have written for the same so experts can check the test class and suggest you on it.

Thanks,
 
Ashwani PradhanAshwani Pradhan
My test class

@isTest
public class CreateProjectTaskBatchJobTest {
    @isTest static void myTest() {
        Account acc = New Account();
        acc.Name = 'Test Account';
        acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(Constants.CUSTOMER_RECORDTYPE_DN).getRecordTypeId();
        acc.SBQQ__TaxExempt__c = 'Yes';
        INSERT acc;
        Opportunity opp = New Opportunity();
        opp.name = 'Test';
        opp.CloseDate = System.today();
        opp.stageName = 'Discovery';
        INSERT opp;
        Order testOrder2 = new Order();
        testOrder2.Name = 'Test Order ';
        testOrder2.Status = 'Draft';
        testOrder2.OpportunityId = opp.id;
        testOrder2.EffectiveDate = system.today();
        testOrder2.EndDate = system.today() + 4;
        testOrder2.AccountId = acc.Id;
        testOrder2.blng__BillingDayOfMonth__c = '1';
        testOrder2.Pricebook2Id = Test.getStandardPricebookId();
        testOrder2.Type = 'Asset';
        INSERT testOrder2;
        
        List<String> orderIds = New List<String>();
        orderIds.add(testOrder2.Id);
        CreateProjectTask.createTasks(orderIds);
       
        CreateProjectTaskBatchJob sh1 = new CreateProjectTaskBatchJob();
        
        //SchedulableContext sc = null;
        //CreateProjectTaskBatchJob sh1 = new CreateProjectTaskBatchJob();
        //sh1.execute(sc);
                
        // This test runs a scheduled job at midnight Sept. 3rd. 2022
        String sch = '0 0 0 3 9 ? 2022';
        String jobId = system.schedule('Test check', sch, sh1);
        // Get the information from the CronTrigger API object
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        // Verify the expressions are the same System.assertEquals(CRON_EXP, ct.CronExpression);
        // Verify the job has not run
        System.assertEquals(0, ct.TimesTriggered);
        // Verify the next time the job will run
        System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));      
    }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashwani,

Can you try to populate the Close_Won_Date__c as system.today()+1 and Order_Location__c field in the test data for order. Because the test order is not satisfyiing these conditions it is not covering the remaining part.

Please let me know if you still face the same.

If this solution helps, Please mark it as best answer.

Thanks,
 
Ashwani PradhanAshwani Pradhan
Unfortunately Close_Won_Date__c is not a writable column and I am not sure how to avoid that?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashwani,

Can you check if that is a formula field and let me know what exactly is the formula so we can adjust it based on that.

Thanks,
 
Ashwani PradhanAshwani Pradhan
Yes its formula field and formula is Opportunity.CloseDate
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashwani,

In that scenerio while inserting opportunity you need to populate the close date as 
 
opp.CloseDate = System.today()+10;

And also give the Order_Location__c field while creating the order. This should solve the coverage issue.

Thanks,
 
Ashwani PradhanAshwani Pradhan
Ideally it should pass for my shedulable class as when I am creating order then it should automatically set this Close_Won_Date__c but its not crossing more than 71%.
Ashwani PradhanAshwani Pradhan
opp.CloseDate = System.today()+10;
Did not work.
Custom_Location__c loc = new Custom_Location__c();
        loc.Id = 'a0E1T00000Xbf01UAB';
        testOrder2.Order_Location__c = loc.Id;
I used this for location which is there any issue with this? Even I queired in test clase and its getting one record so inserts are working fine.

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashwani,

You may need to create the Custom_Location__c record as we did for opportuity and order and share us the code coverage screenshot so can check and confirm what part is not covering.

Thanks,
 
Ashwani PradhanAshwani Pradhan
Thank you for putting so much effor Praveen. Sharing screenshot and current test class which covers 71%

User-added image

As of now test class
@isTest
public class CreateProjectTaskBatchJobTest {
	@isTest static void orderTest() { 
        Account acc = New Account();
        acc.Name = 'Test Account';
        acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(Constants.CUSTOMER_RECORDTYPE_DN).getRecordTypeId();
        acc.SBQQ__TaxExempt__c = 'Yes';
        INSERT acc;
        Opportunity opp = New Opportunity();
        opp.name = 'Test';        
        opp.stageName = 'Discovery';
        opp.CloseDate = System.today();
        INSERT opp;
        
        Custom_Location__c loc = New Custom_Location__c();
        loc.Account__c = acc.Id;
        loc.Name = 'Test Location';
        INSERT loc;
        
        Order testOrder2 = new Order();
        testOrder2.Name = 'Test Order ';
        testOrder2.Status = 'Draft';
        testOrder2.OpportunityId = opp.id;
        testOrder2.EffectiveDate = system.today();
        testOrder2.EndDate = system.today() + 4;
        testOrder2.AccountId = acc.Id;
        testOrder2.blng__BillingDayOfMonth__c = '1';
        testOrder2.Pricebook2Id = Test.getStandardPricebookId();
        //testOrder2.Close_Won_Date__c = System.today()+1;
        testOrder2.Type = 'Asset';       
        testOrder2.Order_Location__c = loc.Id;
        INSERT testOrder2;
        
        //System.assert(testOrder2.Order_Location__c != null ,'Order_Location__c field is not null');
        
        List<String> orderIds = New List<String>();
        orderIds.add(String.valueOf(testOrder2.id)); 
        
        CreateProjectTask.createTasks(orderIds);
        
    }
    
    static testmethod void testDailyCreateProjectTaskScheduledJob() {        
        String sch = '0 5 12 * * ?';
        Test.startTest(); 
        String jobId = System.schedule('ScheduleApexText', sch, new CreateProjectTaskBatchJob());         
    }
}