• Mohammad Ahetesham
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I am working on a trail for future method, my method finds the number of contacts on account and updates the "Number of Contacts" field on the Account.

Everyhting is working as expected but in the test class while asserting the output, the SOQL is not pulling this field.

Future Method:
public class AccountProcessor {
    
	@future
    public static void countContacts(List<Id> AccIds){
        
    List<Account> acctsContacts = new List<Account>();
        
    //List<Contact> noOfContacts = new List<Contact>();
        
        //integer noOfContacts;
        
    for(ID accn: AccIds){
            
     //contactsID = [Select ID from Contact where Account.ID = :accn];
            
     //noOfContacts = [Select COUNT() from Contact where Account.ID = :accn];
          
     	acctsContacts =  [Select Name, (Select Firstname, Lastname FROM Contacts)
                          FROM Account
                          WHERE ID =:accn]; 
         
            for(Account accns: acctsContacts){
                
            	integer noOfContacts = accns.Contacts.size();
                System.debug('Number of Contacts' +noOfContacts);
                    
                accns.Number_of_Contacts__c = noOfContacts;                 
                
            	}                        
       
       }        
        
    }

}

Test Method:
 
@isTest
public class AccountProcessorTest {
    
    @isTest
    public static void testcountContacts(){
        
        //Test Data Preparation - Create new accounts
        ID testAccID;
        List<ID> testAccIdsList = new List<ID>();
        List<Account> testAccount = new List<Account>{
            
            new Account(Name ='TestAccount 1'),
            new Account(Name = 'TestAccount 2'),
            new Account(Name = 'TestAccount 3'),
            new Account(Name = 'TestAccount 4')
        };
            
            insert testAccount;  
        
        //Create 2 contact for each Account
        List<Contact>consList = new List<Contact>();
        for (Account eachAccount:testAccount){
            
            for(integer i=0; i<2; i++){
                
               // List<Contact>consList = new List<Contact>{
                    
                  Contact cons = new Contact(Firstname ='Contact'+i, Lastname = 'Test'+i, AccountId = eachAccount.Id);
                  consList.add(cons);
            }
            
        }
        
        insert conslist;
        
        System.debug('Number of Contacts'+conslist.size());
        
        System.debug('Contacts'+conslist);
        
            //Getting the IDs of the Accounts
            for(Account testaccn:testAccount){
                
                //testAccId = [Select ID from Account 
                             //WHERE Account.ID =:testaccn.Id];
                    
                testAccIdsList.add(testaccn.ID);
                
            }
        
        //Test Starts 
        Test.startTest();
         
        AccountProcessor.countContacts(testAccIdsList);
            
        Test.stopTest();
        
           List<Account>accContactPresent = new list<Account>();
        
           System.debug('List of IDs'+testAccIdsList);
           
           for(Id eachaccID :testAccIdsList){
           
                // This SOQL is not pulling the Number_of_Contacts field
                
                Account contactCount = [Select Match_Billing_Address__c, Number_of_Contacts__c from Account 
                                   		WHERE ID= :eachaccID];
               
           		accContactPresent.add(contactCount);
           
           }  
           for(Account foreachContact :accContactPresent){
               
            	System.debug('Contacts Present'+accContactPresent);
            
            	System.assertEquals(2, foreachContact.Number_of_Contacts__c);
               
         }
        
           
    }

}

Log
At line 73: is wehre, I am not able to pull the Number_Of_Contacts field from Account. It is pulling the ID and Match Billing Address field.

Any help would be appreciated.