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
System Administrator 393System Administrator 393 

I am writing a test class to update the field of one of my bill object and I am not getting any code coverage for my for loops which go over a list of contacts and find a contact with a status of Signing Officer

All the tests pass and I have 50% Code Covergae, the highlighted bold text is the one that I do not have code coverage for.

public class UpdateContactfields{

public static Account getAccount(loan__Loan_account_Due_Details__c a) {


 Account a1 = [SELECT Id, Name, CurrencyIsoCode FROM Account WHERE Name =: a.Account_Name__c];
 
 return a1;
 
 }
 
 public static List<Contact> ListofCons(loan__Loan_account_Due_Details__c a){
 
 List<Contact> c = [SELECT AccountId, Name, Email, Title, Contact_Status__c  FROM Contact WHERE AccountId =: getAccount(a).Id ];
 
 return c;
 
 }
 
 public static String setfield(loan__Loan_account_Due_Details__c a1){
 
 
 if((ListofCons(a1)).size()>0){
 for(Contact c2:(ListofCons(a1))){
 if(c2.Contact_Status__c == 'Signing Officer'){
 a1.Signing_Officer__c = c2.Name;
 }
}
}
return a1.Signing_Officer__c;


}
}



@isTest
private class UpdateContacts{

    @isTest static void UpdateContacts_Test() {
    
    Account acc = new Account(Name = 'DEF');
    insert acc;
    
    Account acc1 = new Account(Name = 'PRI');
    insert acc1;
    
    Contact c1 = New Contact();
    c1.LastName = 'ABC';
    c1.Title = 'CEO';
    c1.Contact_Status__c = 'Signing Officer';
    c1.AccountId = acc.Id;
    c1.Email = 'sh123@gmail.com';
    insert c1;
    
    
    Contact c2 = New Contact();
    c2.LastName = 'XYZ';
    c2.Title = 'CFO';
    c2.Contact_Status__c = 'Financial Operations';
    c2.AccountId = acc.Id;
    c2.Email = 'lo123@gmail.com';
    insert c2;
    
    List<Contact> cons1 = new List<Contact>();
    cons1.add(c1);
    cons1.add(c2);
    
    
    Id n1;
    loan__Office_Name__c[] a = [SELECT Name,loan__Office_Name_ID__c,CurrencyIsoCode, loan__Office_Short_Name__c FROM loan__Office_Name__c WHERE Name = 'TIMIA Capital Corporation YVR'Limit 1];
    if(a.size()>0){
    n1 = a[0].Id;
    }
    Id n2;
    loan__Loan_Product__c[] a1 = [SELECT Name, loan__Loan_Product_External_Id__c FROM loan__Loan_Product__c WHERE Name = 'Revenue Loan - HiGrowth STEP UP'Limit 1];
    if(a1.size()>0){
    n2 = a1[0].Id;
    }
    else n2 = null;
    
    loan__Loan_Account__c l1 = new loan__Loan_Account__c(loan__Account__c = acc.Id, loan__Loan_Amount__c = 1000000, loan__Due_Day__c = 5, loan__Interest_Rate__c = 21,loan__Branch__c = n1,loan__Loan_Product_Name__c = n2);
    Database.insert (l1, false);
    loan__Loan_Account__c l2 = new loan__Loan_Account__c(loan__Account__c = acc1.Id, loan__Loan_Amount__c = 1000000, loan__Due_Day__c = 5, loan__Interest_Rate__c = 21,loan__Branch__c = n1,loan__Loan_Product_Name__c = n2);
    Database.insert(l2,false);
    
    
    loan__Loan_account_Due_Details__c b1 = new loan__Loan_account_Due_Details__c(loan__Loan_Account__c = l1.Id);
    System.assertEquals(l1.Id, b1.loan__Loan_Account__c);
    
    loan__Loan_account_Due_Details__c b3 = new loan__Loan_account_Due_Details__c(loan__Loan_Account__c = l2.Id);
    
    
    Database.insert (b1, false);
    Database.insert(b3, false);
    
    
    try {
    loan__Loan_account_Due_Details__c b2 = [SELECT Name, loan__Loan_Account__c, Signing_Officer__c, CurrencyIsoCode, Account_Name__c FROM loan__Loan_account_Due_Details__c WHERE Name =: b1.Name];
    
    Account a2 = [SELECT Id, Name, CurrencyIsoCode FROM Account WHERE Name =: b2.Account_Name__c];
    System.assertEquals(a2,UpdateContactfields.getAccount(b2));
    
    System.assertEquals(acc.Id, UpdateContactfields.getAccount(b2).Id);
    
    System.assertEquals('DEF',UpdateContactfields.getAccount(b2).Name);
    
    System.assertEquals(cons1,(UpdateContactfields.ListofCons(b2)));
    
    System.assertEquals(2,(UpdateContactfields.ListofCons(b2)).size());
    
    System.assert((UpdateContactfields.ListofCons(b2)).contains(c1),true);
    
    System.assert(UpdateContactfields.ListofCons(b2).contains(c2),true);
    
    Contact con1 = (UpdateContactfields.ListofCons(b2))[0];
    System.assertEquals('ABC', (UpdateContactfields.ListofCons(b2))[0].Name);
    System.assert(con1.Contact_Status__c == 'Signing Officer', true);
    System.assert(b2.Signing_Officer__c == null, true);
    System.assertEquals('ABC',UpdateContactfields.setfield(b2));
    System.assertEquals('ABC', b2.Signing_Officer__c);
    
    
    Contact cons2 = (UpdateContactfields.ListofCons(b2))[1];
    System.assertEquals('XYZ', (UpdateContactfields.ListofCons(b2))[0].Name);
    System.assert(cons2.Contact_Status__c == 'Signing Officer', false);
    System.assert(b2.Signing_Officer__c == null, false);
    System.assertEquals('ABC',UpdateContactfields.setfield(b2));
    System.assertEquals('ABC', b2.Signing_Officer__c);
    
    Contact con3 = (UpdateContactfields.ListofCons(b2))[2];
    System.assertEquals(null, (UpdateContactfields.ListofCons(b2))[2]);
    
     
    loan__Loan_account_Due_Details__c b4 = [SELECT Name, loan__Loan_Account__c, Signing_Officer__c, CurrencyIsoCode, Account_Name__c FROM loan__Loan_account_Due_Details__c WHERE Name =: b3.Name];
    System.assertEquals(acc1,UpdateContactfields.getAccount(b4));
    System.assertEquals(0,(UpdateContactfields.ListofCons(b4)).size());
    System.assert(UpdateContactfields.ListofCons(b4).contains(c1), false);
    System.assert(UpdateContactfields.ListofCons(b4).contains(c2), false);
    System.assertEquals(null, UpdateContactfields.setfield(b2));
    System.assertEquals(null, b4.Signing_Officer__c);
    
    
   }
   catch(QueryException e){
   
    
   
   
}   
 
        
}
}