+ Start a Discussion
Tyler LarrabeeTyler Larrabee 

Testing For Loop

Howdy,

I am trying to get 100% code coverage on my test class, but I am failing to write a text class that actually covers the code
 
@isTest
    static void testM3Update() {
        
        Test.setMock(HttpCalloutMock.class, new MockSetupForSpringCM());
        SFDC_Employee__c ee = [Select Id From SFDC_Employee__c];
        dsfs__Docusign_Status__c testDssRecord = new dsfs__Docusign_Status__c();
        testDssRecord.dsfs_Employee__c = ee.Id;
       	testDssRecord.dsfs__Subject__c = 'Welcome Aboard, Reg!';
        testDssRecord.dsfs__Envelope_Status__c = 'Sent';
        testDssRecord.dsfs__Sent_Date_Time__c = date.today();
        
        
        
        
        Test.startTest();
        dsfs__Docusign_Status__c otherDssRecord = new dsfs__Docusign_Status__c();
        otherDssRecord.dsfs_Employee__c = ee.Id;
       	otherDssRecord.dsfs__Subject__c = 'Welcome Aboard, Reg!';
		otherDssRecord.dsfs__Envelope_Status__c = 'Completed';
        otherDssRecord.dsfs__Sent_Date_Time__c = date.today();
        
        
        insert otherDssRecord;
        insert testDssRecord;
        List<SFDC_Employee__c> eeList = [select Id, M3_Upload__c from SFDC_Employee__c where M3_Upload__c = false AND Id =: testDssRecord.Id];
        for(SFDC_Employee__c preEe : eeList) {
			System.assert(!preEe.M3_Upload__c);            
        }
        testDssRecord.dsfs__Envelope_Status__c = 'Completed';
        update testDssRecord;
        List<SFDC_Employee__c> eeUpdated = [select Id, M3_Upload__c from SFDC_Employee__c where M3_Upload__c = true AND Id =: testDssRecord.Id];
        Test.stopTest();
        
        System.assertEquals(eeUpdated.size() > 0, false, 'There was a problem with setting M3 upload');
        for(SFDC_Employee__c m3Ee : eeUpdated) {
			System.assert(m3Ee.M3_Upload__c);            
        }
        
    }
I am having trouble covering the trigger below: 
if(Trigger.isAfter && Trigger.isUpdate) {
        Set<Id> dssSet = trigger.newMap.keySet();
        for(dsfs__Docusign_Status__c d: [Select Id, dsfs_Employee__c, dsfs__Subject__c, dsfs__Envelope_Status__c From dsfs__Docusign_Status__c Where dsfs__Subject__c LIKE 'Welcome Aboard%' AND dsfs_Employee__r.Employee_Status__c = 'A' AND dsfs__Envelope_Status__c = 'Completed' AND dsfs_Employee__r.Location__r.Payroll__c = 'Payroll' AND Id IN: dssSet]) {
            SFDC_Employee__c ee = [Select Id, M3_Upload__c From SFDC_Employee__c Where Id =: d.dsfs_Employee__c];
            ee.M3_Upload__c = true;
            update ee;
        }
    }

I am unable to get coverage on the last 3 lines of code, a simply field update. 

 
Best Answer chosen by Tyler Larrabee
Kancharla ChakravarthyKancharla Chakravarthy
@Tyler

Can you ping me the code where you have set the below fields for SFDC_Employee__c ee record

dsfs_Employee__r.Employee_Status__c = 'A'
dsfs_Employee__r.Location__r.Payroll__c = 'Payroll'


I don't see the above values set for the SFDC_Employee__c ee record. Please set the values and re-execute.

below code is not up to the standards

1) You shouldn't use SOQL inside for loop 
2) No DML operations are allowed inside for loop

Use Map to query the data and get inside for loop

 
if(Trigger.isAfter && Trigger.isUpdate) {
        Set<Id> dssSet = trigger.newMap.keySet();
        for(dsfs__Docusign_Status__c d: [Select Id, dsfs_Employee__c, dsfs__Subject__c, dsfs__Envelope_Status__c From dsfs__Docusign_Status__c Where dsfs__Subject__c LIKE 'Welcome Aboard%' AND dsfs_Employee__r.Employee_Status__c = 'A' AND dsfs__Envelope_Status__c = 'Completed' AND dsfs_Employee__r.Location__r.Payroll__c = 'Payroll' AND Id IN: dssSet]) {
            SFDC_Employee__c ee = [Select Id, M3_Upload__c From SFDC_Employee__c Where Id =: d.dsfs_Employee__c];
            ee.M3_Upload__c = true;
            update ee;
        }
    }



Please choose as best answer if the above code works for you. It will help others to find the best answers

All Answers

Kancharla ChakravarthyKancharla Chakravarthy
@Tyler

Can you ping me the code where you have set the below fields for SFDC_Employee__c ee record

dsfs_Employee__r.Employee_Status__c = 'A'
dsfs_Employee__r.Location__r.Payroll__c = 'Payroll'


I don't see the above values set for the SFDC_Employee__c ee record. Please set the values and re-execute.

below code is not up to the standards

1) You shouldn't use SOQL inside for loop 
2) No DML operations are allowed inside for loop

Use Map to query the data and get inside for loop

 
if(Trigger.isAfter && Trigger.isUpdate) {
        Set<Id> dssSet = trigger.newMap.keySet();
        for(dsfs__Docusign_Status__c d: [Select Id, dsfs_Employee__c, dsfs__Subject__c, dsfs__Envelope_Status__c From dsfs__Docusign_Status__c Where dsfs__Subject__c LIKE 'Welcome Aboard%' AND dsfs_Employee__r.Employee_Status__c = 'A' AND dsfs__Envelope_Status__c = 'Completed' AND dsfs_Employee__r.Location__r.Payroll__c = 'Payroll' AND Id IN: dssSet]) {
            SFDC_Employee__c ee = [Select Id, M3_Upload__c From SFDC_Employee__c Where Id =: d.dsfs_Employee__c];
            ee.M3_Upload__c = true;
            update ee;
        }
    }



Please choose as best answer if the above code works for you. It will help others to find the best answers
This was selected as the best answer
Tyler LarrabeeTyler Larrabee
@Kancharla

I set the employee status and got 100% test coverage. I also adjusted my for loop to remove the DML operations and SOQL queries per your recommendation. Thanks so much for your help!
Location__c loc = new Location__c();
        loc.Name = 'Test';
        loc.Account__c = butt.Id;
        loc.Payroll__c = 'Payroll';
        insert loc;
        
        
        SFDC_Employee__c testEmployee = new SFDC_Employee__c();
        testEmployee.Name = 'Test';
        testEmployee.First_Name__c = 'Test';
        testEmployee.Account__c = butt.id;
        testEmployee.Location__c = loc.id;
        testEmployee.M3_Upload__c = false;
        testEmployee.Employee_Status__c = 'A';
        insert testEmployee;

if(Trigger.isAfter && Trigger.isUpdate) {
        Set<Id> dssSet = trigger.newMap.keySet();
        Map<Id, Id> eeMap = new Map<Id, Id>();
        List<SFDC_Employee__c> eeList = new List<SFDC_Employee__c>();
        for(dsfs__Docusign_Status__c d: [Select Id, dsfs_Employee__c, dsfs__Subject__c, dsfs__Envelope_Status__c From dsfs__Docusign_Status__c Where dsfs__Subject__c LIKE 'Welcome Aboard%' AND dsfs_Employee__r.Employee_Status__c = 'A' AND dsfs__Envelope_Status__c = 'Completed' AND dsfs_Employee__r.Location__r.Payroll__c = 'Payroll' AND Id IN: dssSet]) {
            eeMap.put(d.Id, d.dsfs_Employee__c);
        }
        for(SFDC_Employee__c ee: [Select Id, M3_Upload__c From SFDC_Employee__c Where Id IN: eeMap.values()]) {
            ee.M3_Upload__c = true;
            eeList.add(ee);
        }
        update eeList;
    }