• Victoria Bova
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
I am very new to Salesforce and the world of coding so please be very descriptive with your answers. Here is the code: 

trigger ContactSumTriggers on Contact (After insert, After delete, After undelete, After Update) {
   Set<Id> parentIdsSet = new Set<Id>();
   List<Account> accountListToUpdate = new List<Account>();
   IF(Trigger.IsAfter){
       IF(Trigger.IsInsert || Trigger.IsUndelete  || Trigger.isUpdate){
           FOR(Contact c : Trigger.new){
               if(c.AccountId!=null){   
                  parentIdsSet.add(c.AccountId);
                  If(Trigger.isUpdate){
                      if(Trigger.oldMap.get(c.Id).AccountId != c.AccountId){
                         parentIdsSet.add(Trigger.oldMap.get(c.Id).AccountId);
                      }
                   }
               }
           }
       }
       IF(Trigger.IsDelete){
           FOR(Contact c : Trigger.Old){
               if(c.AccountId!=null){   
                  parentIdsSet.add(c.AccountId);
               }
           }
       }
   }
   System.debug('#### parentIdsSet = '+parentIdsSet);
   List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]);
   FOR(Account acc : accountList){
       List<Contact> contactList = acc.Contacts;
       acc.Number_of_Contacts__c = contactList.size();
       accountListToUpdate.add(acc);
   }
   try{
       update accountListToUpdate;
   }catch(System.Exception e){
       
   }
}
I am very new to Salesforce and the world of coding so please be very descriptive with your answers. Here is the code: 

trigger ContactSumTriggers on Contact (After insert, After delete, After undelete, After Update) {
   Set<Id> parentIdsSet = new Set<Id>();
   List<Account> accountListToUpdate = new List<Account>();
   IF(Trigger.IsAfter){
       IF(Trigger.IsInsert || Trigger.IsUndelete  || Trigger.isUpdate){
           FOR(Contact c : Trigger.new){
               if(c.AccountId!=null){   
                  parentIdsSet.add(c.AccountId);
                  If(Trigger.isUpdate){
                      if(Trigger.oldMap.get(c.Id).AccountId != c.AccountId){
                         parentIdsSet.add(Trigger.oldMap.get(c.Id).AccountId);
                      }
                   }
               }
           }
       }
       IF(Trigger.IsDelete){
           FOR(Contact c : Trigger.Old){
               if(c.AccountId!=null){   
                  parentIdsSet.add(c.AccountId);
               }
           }
       }
   }
   System.debug('#### parentIdsSet = '+parentIdsSet);
   List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]);
   FOR(Account acc : accountList){
       List<Contact> contactList = acc.Contacts;
       acc.Number_of_Contacts__c = contactList.size();
       accountListToUpdate.add(acc);
   }
   try{
       update accountListToUpdate;
   }catch(System.Exception e){
       
   }
}