You need to sign in to do that
Don't have an account?
write a test class for this Trigger,
please give help for this.
trigger countOnContact on Contact(after insert, after delete)
{
Set<id> accid=new Set<id>();
List<Contact> conlist=new List<Contact>();
List<Contact> listcon=new List<Contact>();
List<Account> acclist=new List<Account>();
List<Account> listacc=new List<Account>();
Map<id ,integer> mapCount=new Map<id,integer>();
if(trigger.isInsert)
{
for(Contact con:trigger.new)
{
accid.add(con.Accountid);
}
}
if(trigger.isdelete)
{
for(Contact con:trigger.old)
{
accid.add(con.Accountid);
}
}
acclist=[select id,name from Account where id in:accid];
conlist=[select id,name, accountid from Contact where accountid in:accid];
for(Account acc:acclist)
{
listcon.clear();
for(contact c: conlist)
{
if(c.accountid==acc.id)
{
listcon.add(c);
mapCount.put(c.accountid,listcon.size());
}
}
}
if(acclist.size()>0)
{
for(Account a: acclist)
{
if(mapCount.get(a.id)==null)
a.CountOfContacts__c=0;
else
a.CountOfContacts__c=mapCount.get(a.id);
listacc.add(a);
}
if(listacc.size()>0)
update listacc;
}
}
Thanks
Naveen
trigger countOnContact on Contact(after insert, after delete)
{
Set<id> accid=new Set<id>();
List<Contact> conlist=new List<Contact>();
List<Contact> listcon=new List<Contact>();
List<Account> acclist=new List<Account>();
List<Account> listacc=new List<Account>();
Map<id ,integer> mapCount=new Map<id,integer>();
if(trigger.isInsert)
{
for(Contact con:trigger.new)
{
accid.add(con.Accountid);
}
}
if(trigger.isdelete)
{
for(Contact con:trigger.old)
{
accid.add(con.Accountid);
}
}
acclist=[select id,name from Account where id in:accid];
conlist=[select id,name, accountid from Contact where accountid in:accid];
for(Account acc:acclist)
{
listcon.clear();
for(contact c: conlist)
{
if(c.accountid==acc.id)
{
listcon.add(c);
mapCount.put(c.accountid,listcon.size());
}
}
}
if(acclist.size()>0)
{
for(Account a: acclist)
{
if(mapCount.get(a.id)==null)
a.CountOfContacts__c=0;
else
a.CountOfContacts__c=mapCount.get(a.id);
listacc.add(a);
}
if(listacc.size()>0)
update listacc;
}
}
Thanks
Naveen
Thanks for given this reply.
and one more thing is this code gives 96% code coverage.
this line will not cover 49. a.CountOfContacts__c=0;
Thanks
Naveen
Thank you very much for given to this reply. It is very useful to me.
if you don't mind could you please give me your mailID.
Thanks,
Naveen.
Thanks
Thanks,
Naveen