You need to sign in to do that
Don't have an account?

can some one help me writing test class for this program
trigger NumOfContacts on contact (after insert, after update, after delete, after undelete)
{
List<account> acc = new List<account>();
Set<Id> sid = new Set<Id>();
if(Trigger.isDelete)
{
for(contact con:Trigger.Old)
{
sid.add(con.accountId);
}
}
else
if(Trigger.isUpdate)
{
for(contact con:Trigger.New)
{
sid.add(con.accountId);
}
for(contact con:Trigger.Old)
{
sid.add(con.accountId);
}
}
else
{
for(contact con:Trigger.New)
{
sid.add(con.accountId);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),accountId FROM contact where accountID IN :sid GROUP BY accountID ];
for(AggregateResult ar:groupedResults)
{
Id custid = (ID)ar.get('accountId');
Integer count = (INTEGER)ar.get('expr0');
account cust1 = new account(Id=custid);
cust1.No_Of_Contacts__c = count;
acc.add(cust1);
}
update acc;
}
{
List<account> acc = new List<account>();
Set<Id> sid = new Set<Id>();
if(Trigger.isDelete)
{
for(contact con:Trigger.Old)
{
sid.add(con.accountId);
}
}
else
if(Trigger.isUpdate)
{
for(contact con:Trigger.New)
{
sid.add(con.accountId);
}
for(contact con:Trigger.Old)
{
sid.add(con.accountId);
}
}
else
{
for(contact con:Trigger.New)
{
sid.add(con.accountId);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),accountId FROM contact where accountID IN :sid GROUP BY accountID ];
for(AggregateResult ar:groupedResults)
{
Id custid = (ID)ar.get('accountId');
Integer count = (INTEGER)ar.get('expr0');
account cust1 = new account(Id=custid);
cust1.No_Of_Contacts__c = count;
acc.add(cust1);
}
update acc;
}
Please mark this as BEST ANSWER if it help!
All Answers
public class TestAccountActive
{
static testmethod void testme(){
account a = new account();
a.name = 'Test Name';
insert a;
list<contact> lstcon = new list<>(Contact);
contact c =new contact(lastname = 'siva',Active__c = false,accountid = a.id);
lstcon.add(c);
contact c1 =new contact(lastname = 'siva',Active__c = false,accountid = a.id);
lstcon.add(c1);
insert lstcon;
update lstcon;
}
}
Try this..
Please mark this as BEST ANSWER if it help!