+ Start a Discussion
Cris9931Cris9931 

stuck at 40% code coverage

Hi, I'm stuck with this test class, I can't progress to 75% at least. I'm trying to make a code coverage for this part of the code:

User-added imageCan anyone give me some hints on how can I cover this?

 

My test class:

@isTest
public class scheduledBatchTimeEntryForWorkDetail_UT
{

    static testMethod void testTimeEntryCreation()
    {
        Profile fsmProf = [SELECT Id FROM Profile WHERE Name='SIG-FSM-Field Service Engineer' limit 1];
        User fsmUser = new User(Alias = 'testfsm1', Email='testfsm1@test.com', 
                                EmailEncodingKey='UTF-8', LastName='TestFSM1', LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', ProfileId = fsmProf.Id, 
                                TimeZoneSidKey='America/Los_Angeles', UserName='testfsm1@test.com',
                                SIG_Ticket_Recipient__c = 'AM-N - FSE', isActive=true, SIG_Is_User_Authorized__c = true);
        insert fsmUser;
        
        SVMXC__Service_Group__c serviceTeam = new SVMXC__Service_Group__c();
        serviceTeam.SVMXC__Active__c = True;
        serviceTeam.Name = 'Test Service Group';
        insert serviceTeam;
        
        SVMXC__Service_Group_Members__c tech = new SVMXC__Service_Group_Members__c();
        tech.Name='Test Tech';
        tech.SVMXC__Service_Group__c = serviceTeam.id;
        tech.SVMXC__Salesforce_User__c = fsmUser.Id;
        insert tech;
        
        //insert work order
        SVMXC__Service_Order__c  workOrder = new SVMXC__Service_Order__c ();
        workOrder.SVMXC__Order_Status__c  = 'Open';
        workOrder.SVMXC__Sub_Status__c = 'Unresolved';
        workOrder.SVMXC__Priority__c = 'Low';
        workOrder.SVMXC__Order_Type__c = 'Reactive';
        workOrder.SVMXC__Group_Member__c  = tech.Id;
        insert workOrder; 
        

        
        //insert work detail
        SVMXC__Service_Order_Line__c woDetail = new SVMXC__Service_Order_Line__c();
        Id workDetailRecordTypeId = Schema.SObjectType.SVMXC__Service_Order_Line__c.getRecordTypeInfosByName().get('Usage/Consumption').getRecordTypeId();
        woDetail.RecordTypeId = workDetailRecordTypeId;
        woDetail.SVMXC__Service_Order__c = workOrder.Id;
        woDetail.SVMXC__Line_Type__c ='Labor';
        woDetail.SVMXC__Line_Status__c = 'Processed';
        woDetail.SVMXC__Start_Date_and_Time__c = system.today();
        woDetail.SVMXC__End_Date_and_Time__c = system.today()+1;
        woDetail.SVMXC__Group_Member__c =  tech.Id;
        insert woDetail; 
        
        Id workDetailTechnicianId = woDetail.SVMXC__Group_Member__c;

        //insert timesheet
        SVMXC__Timesheet__c weekelyTimeEntries = new SVMXC__Timesheet__c();
        weekelyTimeEntries.SIG_Timesheet_Technician__c =   workDetailTechnicianId;
        weekelyTimeEntries.SVMXC__Start_Date__c = System.Today();
        weekelyTimeEntries.SVMXC__User__c = UserInfo.getUserId();
        insert weekelyTimeEntries;
        
        
        //insert dailyEntries
        SVMXC__Timesheet_Day_Entry__c dayEntries = new SVMXC__Timesheet_Day_Entry__c();
        dayEntries.SVMXC__Timesheet__c = weekelyTimeEntries.Id;
        dayEntries.SVMXC__Timsheet_Day__c = System.Today();
        insert dayEntries;

        //insert time entries
        SVMXC__Timesheet_Entry__c timeEntries = new SVMXC__Timesheet_Entry__c();
        timeEntries.SVMXC__Timesheet__c = weekelyTimeEntries.Id;
        timeEntries.SVMXC__Timesheet_Day__c = dayEntries.Id;
        timeEntries.SVMXC__Work_Detail__c = woDetail.Id;
        timeEntries.SVMXC__Start_Time__c = woDetail.SVMXC__Start_Date_and_Time__c;
        timeEntries.SVMXC__End_Time__c = woDetail.SVMXC__End_Date_and_Time__c;
        timeEntries.SIG_Is_Billable__c  = true;  
        insert timeEntries;

        
        // This test runs a scheduled job at midnight Sept. 3rd. 2022
        String CRON_EXP = '0 0 0 3 9 ? 2022';
        // Schedule the test job
        String jobId = System.schedule('scheduledBatchTimeEntryForWorkDetail_UT', CRON_EXP, new scheduledBatchTimeEntryForWorkDetail());
        // 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));   
             
     
    }
}


the schedule apex class:

global class scheduledBatchTimeEntryForWorkDetail implements Schedulable 
    {
        global void execute(SchedulableContext sc) 
        {
            List<SVMXC__Service_Order_Line__c> workDetails = new List<SVMXC__Service_Order_Line__c>(); 
            Set<Id> techIds = new Set<Id>(); 
                 // techId = '00001' which is for Diego Technician - Salesforce ID
            
            //run the script for all work details that are in the range of 6 months
            DateTime refDate = DateTime.Now();
            DateTime EndDateInterval =  refDate.AddDays(-180);
     
            for(SVMXC__Service_Order_Line__c wd : [SELECT Id, SVMXC__Group_Member__r.SVMXC__Salesforce_User__c,SVMXC__Start_Date_and_Time__c, SVMXC__End_Date_and_Time__c,SVMX_Duration__c, SVMXC__Line_Type__c,                                        SIG_Activity_type__c, SIG_Ship_To__c, 
                                          (SELECT Id FROM SVMXC__Time_Entrys__r)  FROM SVMXC__Service_Order_Line__c            
                                           WHERE (SVMXC__Line_Type__c='Labor' OR SVMXC__Line_Type__c='Travel')           
                                                 AND SVMXC__Group_Member__c!=null AND SVMXC__Line_Status__c = 'Processed'           
                                                 AND (SVMXC__Start_Date_and_Time__c <= :refDate AND SVMXC__Start_Date_and_Time__c >= :EndDateInterval)]) 
            {     
                if(wd.SVMXC__Time_Entrys__r.size() == 0) 
                {   
                    workDetails.add(wd);   
                    techIds.add(wd.SVMXC__Group_Member__r.SVMXC__Salesforce_User__c);  
                } 
            } 
    System.debug('### workDetails : ' + workDetails.size());
    
            Map<String, SVMXC__Timesheet_Day_Entry__c> dayEntries = new Map<String, SVMXC__Timesheet_Day_Entry__c>(); 
     
            for(SVMXC__Timesheet_Day_Entry__c tde : [SELECT Id, SVMXC__Timesheet__c, SVMXC__Timesheet__r.SVMXC__User__c, SVMXC__Timsheet_Day__c FROM SVMXC__Timesheet_Day_Entry__c WHERE SVMXC__Timesheet__r.SVMXC__User__c IN :techIds]) 
            {  
                String key = '' + tde.SVMXC__Timesheet__r.SVMXC__User__c + tde.SVMXC__Timsheet_Day__c;
                dayEntries.put(key, tde); 
            } 

    System.debug('### dayEntries : ' + dayEntries.size()); 
     
     
            List<SVMXC__Timesheet_Entry__c> timeEntries = new List<SVMXC__Timesheet_Entry__c>(); 
    
            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);  
                } 
            } 
     
    System.debug('### timeEntries : ' + timeEntries.size()); 
     
            if(timeEntries.size() > 0)  
                insert timeEntries;
        }
    }

​​​​​​​
SwethaSwetha (Salesforce Developers) 
HI Christian,
Recommend reviewing similar posts from past https://developer.salesforce.com/forums/?id=9062I000000IQTmQAO
https://developer.salesforce.com/forums/?id=906F0000000kGjEIAU
https://salesforce.stackexchange.com/questions/94427/how-to-cover-for-loop-in-test-class

If this information helps, please mark the answer as best. Thank you
pablo Emilio Cruz Rojaspablo Emilio Cruz Rojas
Hi I would like to know if you cn help checking this code to know errors and how i can correct that or make a better method 


public class MyPageController {
    public void recalculateTotals() {
        List<MyCustomObject__c> myCustomObjects = [SELECT Id, Totals__c, Recalculate__c, Value1__c FROM MyCustomObject__c];
        for (MyCustomObject__c obj : myCustomObjects ) {
            if (obj.Recalculate__c) {
                obj.Recalculate__c = false;
                obj.Totals__c = obj.Value1__c + obj.Value2__c;
                update obj;
            }
        }
    }
}

private class PageControllerTest {
    public void recalculateTotalsTest() {  

                 //complete
        }
}