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
Bharat Seth 12Bharat Seth 12 

Anyone help me write test class to make 100 percent coverage for below trigger

Trigger ContactTrigger on Contact(before insert, after insert, before update, after update, before delete, after delete) {
    Set<Id> accountIds = new Set<Id>();
    Set<Id> contactIds = new Set<Id>(); 
    if (Trigger.isBefore) {
        if (Trigger.isInsert) {
            List<Id> accID = new List<Id>();
            for(contact c : trigger.new){
                if(c.accountId != null && c.isPrimary__c==true){
                    accID.add(c.accountId);
                }
            }
            
            List<account> acc = [select id, (select id, isPrimary__c from contacts where isPrimary__c= true) from Account WHERE Id In: accID];
            Map<id, boolean> bool = new map<id,boolean>();
            for(account a : acc){
                bool.put(a.id, a.contacts.size()>0 ? true : false);
            }
            
            for(Contact c : trigger.new){
                if(bool.get(c.AccountId) == true){
                    c.addError(System.Label.Is_Primary_Contact);
                }
            }
            
        } 
        if (Trigger.isUpdate) {
            List<Contact> ConList = New List<Contact>();
            for(Contact con : trigger.new){
                if(con.isPrimary__c != Trigger.oldMap.get(con.Id).isPrimary__c && con.isPrimary__c == true && con.AccountId !=null){
                    accountIds.add(con.AccountId);
                    ConList.add(con);
                }
                if(accountIds.size()>0)
                    ContactTriggerHandler.BeforeUpdate(accountIds, ConList);  
                
            }
        }
        if (Trigger.isDelete) {
            // Call class logic here!
        }
    }
    
    if (Trigger.IsAfter) {
        if (Trigger.isInsert) {
            
        } 
        if (Trigger.isUpdate) {
        }
        if (Trigger.isDelete) {
            // Call class logic here!
        }
    }
}
Naren9Naren9
Hi Bharath,
Below test class will give the 100% code coverage:
Your Class requires that:
1. New Contact should be created with is primary flag =true and should be assigned to a Account.
2. Then Contact should be updated.
3. Create a New Contact under the same Account with isPrimaryFlag = false and update the isPrimaryFlag=true;


@isTest
public class SFDC_ContactTriggerTest {
    @isTest static void CreateNewPrimaryContact(){
        Account a = new Account(Name='TestTriggerMarch182018',Rating='Hot');
        insert a;
        Contact c = new Contact(FirstName='TestTriggerMarch182018',LastName='TestTriggerMarch182018LastName',isPrimary__c=true,AccountId=a.Id);
        insert c;
        c.LastName='TestTriggerMarch182018LastName1111';
        c.isPrimary__c= false;
        update c;
        
        Contact c1 = new Contact(FirstName='TestTriggerMarch1820182nd',LastName='TestTriggerMarch182018LastName2nd',isPrimary__c=false,AccountId=a.Id);
         insert c1;
        c1.isPrimary__c= true;
        update c1;
    }

}

Thanks,
Narendar