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
Malli GMalli G 

codecoverage on trigger iam getting only 66% can any one helpme

i wrote this code;

trigger contactstrigger on Contact (after insert,after delete) {
  if(Trigger.isInsert && Trigger.isAfter)
        TriggerContact.increment(Trigger.New);
    else
        TriggerContact.increment(Trigger.Old);

public class TriggerContact {
    public static void increment(List<Contact> cons){
        set<Id> accid=new Set<id>();
        for(Contact c:cons){
           accId.add(c.AccountId); 
        }
       List<Account> accs=[select id,count_of_contact__c ,(select id from Contacts ) from Account where id in:accId];
        for(Account a:accs){
            a.count_of_contact__c=a.contacts.size();
        }
        update accs;
    }

}

testclass for this code
istest
public class testforcontact {
@istest
    static void testme(){
        account a1=new account(name='aaa',phone='9908294936');
insert a1;
contact c1=new contact(lastname='suresh', firstname='malli',accountid=a1.id);
insert c1;    
        
        list<account>acc=[select id,count_of_contact__c,(select id,lastname from contacts) from account where id=:c1.id];
        for(account a:acc){
            a.count_of_contact__c=a.contacts.size();
        }
        update acc;
    }
}










 
Mahesh DMahesh D
Hi Malli,

Below is the Test Class:
 
@isTest
public class testforcontact {
	@istest
    static void testme(){
        account a1=new account(name='aaa',phone='9908294936');
		insert a1;
		contact c1=new contact(lastname='suresh', firstname='malli',accountid=a1.id);
		insert c1;    
        Account acc = [Select Id, count_of_contact__c from Account where ID =: a1.Id LIMIT 1];
		System.assertEquals(1, acc.count_of_contact__c);
        delete c1;
		Account acc = [Select Id, count_of_contact__c from Account where ID =: a1.Id LIMIT 1];
		System.assertEquals(0, acc.count_of_contact__c);
    }
}

Please do let me know if it helps you.

Regards,
Mahesh
Mahesh DMahesh D
Hi Malli,

Below is the code which is used for real roll up summary calculation:
 
trigger PrimaryContactCount on Contact (after insert, after delete, after undelete,after update) {
    set<Id> accIds = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Contact con: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (con.AccountId != Trigger.oldMap.get(con.Id).AccountId))
				accIds.add(con.AccountId);
        }
    }
    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Contact con: Trigger.old){
            if(Trigger.isDelete || (con.AccountId != Trigger.oldMap.get(con.Id).AccountId))
				accIds.add(con.AccountId);
        }
    }    
    
	if(!accIds.isEmpty()) {
		List<Account> accList = [select id, Count_of_Contact__c, (Select Id from Contacts) from Account Where ID IN: accIds];
		
		for(Account acc : accList){
			system.debug('Contacts--->'+acc.contacts.size());
			acc.Count_of_Contact__c = acc.contacts.size();
		}
		update accList;
	}
}

Please do let me know if it helps you.

Regards,
Mahesh
Malli GMalli G
thank you mahesh i got 100%codecoverage it is very useful to me
Mahesh DMahesh D
Hi Malli,

Please mark it as solved, so that it will be helpful for others to follow the solution.

Regards,
Mahesh