1

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);
    }
}
3
  • what you have tried so far May 11, 2020 at 6:33
  • 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; } }
    – Jeba Roji
    May 12, 2020 at 7:38
  • this code give me total number of child record , and i am not able to findout deleted child record.
    – Jeba Roji
    May 12, 2020 at 7:40

1 Answer 1

0

Update Your handler class with below code

public class update_no_of_contact {
public static void ondelete(List<contact> con) {

    set<id> accid=New set<id>();
    Map<Id, List<Contact>> accountContactsMap = new Map<Id, List<Contact>> ();

    for(contact c:con)
    {
        if (String.isNotBlank (c.AccountId)) {
            accid.add(c.AccountId);
            if (accountContactsMap.containsKey(c.AccountId)) {
                accountContactsMap.get (c.AccountId).add (c);
            } else {
                accountContactsMap.put (c.AccountId, new List<Contact> {c});
            }
        }

    }
    list<account> acclist=new list<Account>();

    for(Account a :[select id,No_of_Contacts__c  from Account where id=:accid])
    {
        a.No_of_Contacts__c = a.No_of_Contacts__c == null 
                            ? accountContactsMap.get (a.Id).size () 
                            : a.No_of_Contacts__c + accountContactsMap.get (a.Id).size ();

        acclist.add(a);
    }
    update acclist;
}
}
3
  • Hi Sudarshan,Thank you so much for your reply, Now its working.
    – Jeba Roji
    May 14, 2020 at 7:32
  • Sudarshan, on Below code after for loop i am not able to understand code is there any other way that we can write code to achieve requirement. for(Account a :[select id,No_of_Contacts__c from Account where id=:accid]) { a.No_of_Contacts__c = a.No_of_Contacts__c == null ? accountContactsMap.get (a.Id).size () : a.No_of_Contacts__c + accountContactsMap.get (a.Id).size (); acclist.add(a); }
    – Jeba Roji
    May 14, 2020 at 7:35
  • I m just checking the existing count and adding it
    – Sudarshan
    May 14, 2020 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .