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
Sana123Sana123 

how to write test class for this code?

public static void countContactDetails(List<Contact> listContacts, Map<Id,Contact> mapOldContacts){
     Set<Id> setAccountId = new Set<Id>();
     Map<Id,Account> mapOfAccount = new Map<Id,Account>();
        if( mapOldContacts == null || mapOldContacts.isEmpty() ) {
            for( Contact objCont : listContacts ) {
                if( objCont.AccountId != null ) {
                    setAccountId.add( objCont.AccountId );
                }
            }
        }
        else if( mapOldContacts != null && !mapOldContacts.isEmpty()) {
            for( Contact objCont : listContacts ) { 
                if( objCont.AccountId != null 
                && mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
                    setAccountId.add( objCont.AccountId );
                    if( mapOldContacts.get( objCont.Id ).AccountId != null ) {
                        setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
                    }
                }
                else if( objCont.AccountId == null && mapOldContacts.get( objCont.Id ).AccountId != objCont.AccountId ) {
                    setAccountId.add( mapOldContacts.get( objCont.Id ).AccountId );
                }
            }
        }
        if( !setAccountId.isEmpty() ) {
            List<Account> listAccount = new List<Account>();
            for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount 
                                       FROM Contact
                                       WHERE AccountId in: setAccountId 
                                       GROUP BY AccountId]){
                                           Account a = new Account();
                                           a.Id = (Id) ar.get('AcctId');
                                           a.Total_Contacts__c = (Integer) ar.get('ContactCount');
                                           listAccount.add(a);

            
        }
   if( !listAccount.isEmpty() ) {
                update listAccount;
            }  
 }
    
             if( !setAccountId.isEmpty() ) {
                  List<Account> listAccount1 = new List<Account>();
            List<AggregateResult> results = [Select AccountId ,Count(Id) ContactCount, Contact_Status__c str from Contact  WHERE AccountId in: setAccountId GROUP BY AccountId, Contact_Status__c];
                  Integer activecount = 0;
                  Integer inactivecount = 0;
                 for (AggregateResult ar : results)  {
                    if(ar.get('str')=='Active'){
                           
                        activecount += (Integer)ar.get('ContactCount');
                     }
                      if(ar.get('str')=='Inactive'){
                          inactivecount = (Integer)ar.get('ContactCount');  
                     }
                     //a.Total_Contacts__c = (Integer) ar.get('ContactCount');
                    // mapOfAccount.put((ID)ar.get('AccountId') , new Account(Id = (Id)ar.get('AccountId'),Total_Active_Contacts__c = activecount ,Total_Inactive_Contacts__c = inactivecount));
                      Account a1 = new Account();
                       a1.Id = (Id) ar.get('AccountId');
                       a1.Total_Active_Contacts__c = activecount;
                     a1.Total_Inactive_Contacts__c = inactivecount;
                  listAccount1.add(a1);
                 }

system.debug('Mapofaccount=='+ listAccount1);
    
 }
    } 
CharuDuttCharuDutt
Hi Sana
Try Below Test Class
@isTest
public class contactDetailTest {
@isTest
    public Static Void UnitTest(){
        list<Contact> lstCon = new list<Contact>();
        Map<Id,Contact> mapOldContacts = new Map<Id,Contact>();
        Account Acc = new Account();
        Acc.Name = 'Test Acc';
        Insert Acc;
        Account Acc2 = new Account();
        Acc2.Name = 'Test Acc 2';
        Insert Acc2;
        Contact Con = new Contact();
        Con.FirstName = 'Test';
        Con.LastName = 'Con';
        Con.AccountId = Acc.Id;
        Con.Contact_Status__c = 'Active';
        Insert Con;
        lstCon.add(con);
        mapOldContacts.put(Con.Id,Con);
        Contact Con2 = new Contact();
        Con2.FirstName = 'Test';
        Con2.LastName = 'Con';
        Con2.AccountId = Acc.Id;
        Con.Contact_Status__c = 'Inactive';
        Insert Con2;
        Con.AccountId = Acc2.Id;
        Update Con2;
        lstCon.add(con2);
        mapOldContacts.put(Con2.Id,Con2);
        
        contactDetail.countContactDetails(lstCon, null);
        contactDetail.countContactDetails(lstCon, mapOldContacts);
    }
}
Please Mark It As Best Answer If it Helps
Thank You!
Sana123Sana123
i want to write test class for bulk testing?
Sana123Sana123
also it is not covering my whole code?
Can yu plese help me
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please do some needful changes according to your requirement. I think this below code would cover 75%

@isTest
public class DataTest {

    @isTest
    public static void countContactDetailsTest()
    {
	Account ac=new Account();
	ac.name='test account';
	insert ac;
	
	contact con=new contact();
	con.accountid=ac.id;
	con.lastName='Test contact';
	List<Contact> contactList=new List<Contact>();
	contactList.add(con);
	
	
	Account acc=new Account();
	acc.name='test account Data';
	insert acc;
	
	contact conn=new contact();
	conn.accountid=acc.id;
	conn.lastName='Test contactt';
	 
	contactList.add(conn);
	insert contactList;
	map<Id,Contact> mapName=new map<Id,Contact>();
	mapName.put(conn.id,conn);
	
	 Test.startTest();
                  className.countContactDetails(contactList,mapName);     			 
     Test.stopTest();
	
	
	}
	
	}

Please mark it as the Best Answer if it helps you 

Thank You

Sana123Sana123
can you please help me ?? i have a issue ..i want to know how can i move my contact from one accout to  lookup account field
Please help me...

Thank you in advance