I have one field on Account object let say No_of_deleted_contact__c ,and there is lookup relationship between Account and Contact , when contact is deleted then Number of deleted contact should be display in No_of_deleted_contact__c.How i can achieve this usecase using Trigger?
I have tried this code but below code display total number of child record .
public class update_no_of_contact {
public static void ondelete(List<contact> con)
{
set<id> accid=New set<id>();
for(contact c:con)
{
accid.add(c.AccountId);
}
list<account> acclist=new list<Account>();
for(Account a :[select id,(select AccountId from contacts) from Account where id=:AccId])
{
a.No_of_Contacts__c=a.contacts.Size();
acclist.add(a);
}
update acclist;
}
}
Trigger-->
trigger updatenoofcontact on Contact (after delete) {
if(Trigger.isafter && Trigger.isdelete)
{
update_no_of_contact.ondelete(Trigger.old);
}
}