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 

test class for this code with using asserts

public class ContactTriggerHandler {
   
     public static void getContactDetails(List<Contact> listOfContacts ,Map<Id,Contact> mapOfContact)
     {
      Set<Id> setAccIds = new Set<Id>();
         for(Contact con: listOfContacts != null ? listOfContacts :mapOfContact.values()){
             if(mapOfContact == null && con.AccountId != null){
                 setAccIds.add(con.AccountId);
             }
            if(listOfContacts  != null && mapOfContact != null 
                     && (con.AccountId != mapOfContact.get(con.Id).AccountId || con.FirstName!=mapOfContact.get(con.Id).FirstName
                         || con.LastName != mapOfContact.get(con.Id).LastName)){            
                if(con.AccountId != null)
            {
                setAccIds.add(con.AccountId);
                
            }
             if(mapOfContact.get(con.Id).AccountId != null)
             {
              setAccIds.add(mapOfContact.get(con.Id).AccountId);   
             }
                         }
             if(listOfContacts == null && con.AccountId != null){
                   setAccIds.add(con.AccountId);
             }
         }   
             if(setAccIds.size() > 0){
                 Map<Id , Account> mapOfAccount = new Map<Id , Account>();
                 for(Contact con1: [SELECT id, AccountId , Name FROM Contact WHERE AccountId IN : setAccIds]){
                     if(!mapOfAccount.containskey(con1.AccountId)){
                         mapOfAccount.put(con1.AccountId, new Account (Id = con1.AccountId , Contact_Details__c =''));
                     }
                         mapOfAccount.get(con1.AccountId).Contact_Details__c += con1.Name + '\n';
                     
                     if(mapOfAccount.size() > 0){
                        update mapOfAccount.values();
                     }
                        
                 }
             }
         
          }
}
    
         

         
Best Answer chosen by Sana123
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can take reference from the below code:
// Assuming for Insert trigger you can also use update
@istest
public class ContactTriggerhandlerTest {
@istest
    public static void testingCon(){
        
        account a =new account();
        a.name='tets';
        insert a;
      List<contact> conList = new List<contact>();
        Contact c=new contact();
        c.firstName = 'Test';
        c.lastName = 'Demo';
        c.AccountId = a.id;
        conList.add(c);     	 

       Contact c1=new contact();
        c1.firstName = 'Test1';
        c1.lastName = 'Demo1';
        c1.AccountId = a.id;
       conList.add(c1);

Test.startTest();
insert ConList;
Test.stopTest();

System.assertEquals(2, Conlist.size(), 'Size is 2');
        
    }
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 

All Answers

Maharajan CMaharajan C
Please post you trigger also so it will be very easy to help you...


Thanks,
Maharajan.C
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can take reference from the below code:
// Assuming for Insert trigger you can also use update
@istest
public class ContactTriggerhandlerTest {
@istest
    public static void testingCon(){
        
        account a =new account();
        a.name='tets';
        insert a;
      List<contact> conList = new List<contact>();
        Contact c=new contact();
        c.firstName = 'Test';
        c.lastName = 'Demo';
        c.AccountId = a.id;
        conList.add(c);     	 

       Contact c1=new contact();
        c1.firstName = 'Test1';
        c1.lastName = 'Demo1';
        c1.AccountId = a.id;
       conList.add(c1);

Test.startTest();
insert ConList;
Test.stopTest();

System.assertEquals(2, Conlist.size(), 'Size is 2');
        
    }
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
This was selected as the best answer
Sana123Sana123
# trigger code

trigger ContactTrigger on Contact (after insert, after update, after delete) {

   
    If(trigger.isAfter &&(Trigger.isInsert || Trigger.isUpdate)){
       ContactTriggerHandler.getContactDetails(trigger.new,trigger.oldMap);
    }
     If(trigger.isAfter &&Trigger.isDelete){
       ContactTriggerHandler.getContactDetails(trigger.new,trigger.oldMap);
    }             
                  
       
   
   }
Maharajan CMaharajan C
Please try the below test class:
 
@isTest
public class ContactTriggerHandlerTest {
    @isTest static void getContactDetailsTest(){

        // Add if there is any other fields are required for Account
        Account acc = new Account(name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for Account
        Account acc1 = new Account(name = 'Test Account1');
        insert acc1;
        
        // Add if there is any other fields are required for Contact 
        Contact con = new Contact(lastName = 'Ryan', AccountId = acc.Id);
        insert con;
        
        Account accRec = [Select Id,Contact_Details__c from Account where Id =: acc.Id];
        system.assertEquals('Ryan', accRec.Contact_Details__c);
        
        Contact con2 = new Contact(lastName = 'Jack',accountId = acc1.Id,firstName = 'Test');
        insert con2;
        con2.firstName = 'Test1';
        con2.lastName = 'Jack1';
        con2.AccountId = acc.Id;
        update con2;    
        Test.startTest();
        Account accRec1 = [Select Id,Contact_Details__c from Account where Id =: acc.Id];
        system.assert(accRec1.Contact_Details__c != null);
        
        delete con2;
        
        Account accRec2 = [Select Id,Contact_Details__c from Account where Id =: acc.Id];
        system.assertEquals('Ryan', accRec2.Contact_Details__c);
		Test.stopTest();
    }
}

Thanks,
Maharajan.C