You need to sign in to do that
Don't have an account?
Cris9931
Test class question for schedule apex
Hi, can anyone give me some feedback at how can I cover these lines of code in my test class?
for(SVMXC__Service_Order_Line__c wd : workDetails) { String key = '' + wd.SVMXC__Group_Member__r.SVMXC__Salesforce_User__c + wd.SVMXC__Start_Date_and_Time__c.date(); SVMXC__Timesheet_Day_Entry__c tde = dayEntries.get(key); if(tde != null) { SVMXC__Timesheet_Entry__c timeEntry = new SVMXC__Timesheet_Entry__c(); timeEntry.SVMXC__Timesheet__c = tde.SVMXC__Timesheet__c; timeEntry.SVMXC__Timesheet_Day__c = tde.Id; timeEntry.SVMXC__Work_Detail__c = wd.Id; timeEntry.SVMXC__Start_Time__c = wd.SVMXC__Start_Date_and_Time__c; timeEntry.SVMXC__End_Time__c = wd.SVMXC__End_Date_and_Time__c; timeEntry.SVMXC__Comments__c = wd.SIG_Activity_type__c; timeEntry.SIG_Is_Billable__c = true; timeEntry.SVMXC__Account__c = wd.SIG_Ship_To__c; timeEntry.SVMXC__Duration__c = wd.SVMX_Duration__c / 60; timeEntries.add(timeEntry); }From line 2 above the code is red and is not covered and I can't understand how to cover it.
Found the solution:
I had to put @isTest (SeeAllData = true) because the records were on Private mode for the object! Crazy.
All Answers
Found the solution:
I had to put @isTest (SeeAllData = true) because the records were on Private mode for the object! Crazy.
I would recommend not to use @isTest(SeeAllData =true) as this is not the Best Practice. Click here to read Apex Documentation for seeAllData (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm)
Instead you can create a test record in your test class and then use that record your test class. For this purpose you can levearage:
@testSetup static void methodName() {
}
Click here to read more about one time test record setup (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm)
I would recommend to follow Test Classes (https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_data) module to get good understanding of test class and you'll easily understand it.
Thanks,
Ashish Singh.
Hi Ashish,
Thanks for that.. Now I have another issue on the same class, the test class runs perfectly I have 100% but when I try to migrate to prod I have this error:
Do I need to re-write that query?
The reason why you got this error because you still have seeAllData = true in your test class. That why it's not a good practice. It will simply query all the records and clearly exceed 50000 records if criteria matches with SOQL.
Secondly, Do you want to do Bulk Processing? If that is the case then you should go with Batch Apex and then schedule that Batch Apex with Apex Scheduler. Otherwise, you'll keep getting this error whenever you have more than 50000 records.
As a workaround if you LIMIT Query by 50000, you'll be able to prevent from the Error.
But a good solution should follow the step mentioned in Bold.
Thanks,
Ashish Singh.
I've transformed in a batch class:
Is this the right approach?
My test class:
I have 100% code coverage with this.