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
divya gourigari 14divya gourigari 14 

how to cover forloop in test class in salesforce

User-added image


@isTest(SeeAllData=true)
public class emailmatchtestclass 
{
    static testMethod void emilmatch(){
        ContactRelationship__c condata=new ContactRelationship__c();
        condata.Name='divya';
        condata.EmailId__c='update234@gmail.com';
        
        insert condata;
        Contact con=new Contact();
        con.Id=condata.ListofContacts__c;
        con.LastName='divya223';
        con.Email='update234@gmail.com';
        con.Email=condata.EmailId__c;
        system.assertEquals(condata.EmailId__c, 'update234@gmail.com');
        con.Opt_out_status__c=true;
        system.assertEquals(condata.EmailId__c, con.Email);
        test.startTest();
        insert con;
        test.stopTest();
        
        
        
    }
}
Best Answer chosen by divya gourigari 14
*hunter*hunter
From what I am seeing your trigger is on ContactRelationship__c which will run as soon as  insert condata is executed. But at that point no contacts are present hence for loop cannot be covered.
The way to achieve (just for the sake of achieving) would be to update the condata record after insert con. 
I would advise you to keep the trigger logic in separate helper class and make the logic modular( eg. separation of before insert and before update).