You need to sign in to do that
Don't have an account?
kanchan tyagi 2
test class of count no of conatct trigger
trigger contactCounttriger on Contact (After insert, After Delete, After Undelete,after update) { Set<id> setAccountId=new Set<id>(); if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate){ for(Contact con:Trigger.New){ setAccountId.add(con.AccountId); } } if(Trigger.IsDelete){ for(Contact con:Trigger.old){ setAccountId.add(con.AccountId); } } List<Account> listAcc=[select id,no_of_contacts__c,(Select id from Contacts)from Account where id in:setAccountId]; for(Account acc:listAcc){ acc.no_of_contacts__c=acc.contacts.size(); } update listAcc; }
Please check below test class
Thanks,
Dhanya
@isTest
public class nocTest{
public static testmethod void count()
{
list<account> acc=new list<account>();
acc.add(new account(Name='anuj'));
acc.add(new account(Name='kanchan'));
Insert acc;
list<Contact> con=new list<Contact>();
con.add(new Contact(lastName='anuj', accountid=acc[0].id));
con.add(new Contact(lastName='kanchan', accountid=acc[0].id));
insert con;
update con;
delete con[1];
}
}
Thanks..
mark it solved so that other can take benifit from this