function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HobieHobie 

Trigger Not Firing when updating a contact

I have a trigger that works when adding and deleting records, it is not firing when updating a record.

 

Below is the trigger.  Thanks for your help!

 

trigger ContactsOnAccount on Contact (after insert, after delete,after undelete,after update) {

    Set<Id> aId = new Set<Id>();
    
    if(Trigger.isInsert || Trigger.isUndelete){
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Total_Number_of_Contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.Total_Number_of_Contacts__c=con.size();
            
        }update acc;
    }
    
    if(Trigger.isDelete){
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,Total_Number_of_Contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.Total_Number_of_Contacts__c=con.size();
            
        }update acc;
    }
   
    if(Trigger.isUpdate){
       Set<Id> OldAId = new Set<Id>(); 
        for(Contact opp : Trigger.new){
        if(opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c)
            aId.add(opp.AccountId);
            OldAId.add(Trigger.oldMap.get(opp.id).AccountId);
        
        }
        if(!aId.isEmpty()){
        //for new Accounts
        List<Account> acc = [select id,Total_Number_of_Contacts__c from Account where Id in:aId];
        //For New Account Contacts
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        /*
        This is For Old Contacts Count
                              */
        
        //for Old Accounts
        List<Account> Oldacc = [select id,Total_Number_of_Contacts__c from Account where Id in:OldAId];
        
        //For Old Account Contacts
        List<Contact> OldCon = [select id from contact where AccountId in :OldAId];
       
        //For New Accounts
        for(Account a : acc){
            a.Total_Number_of_Contacts__c=con.size();
            
            
        }update acc;
        
        //For Old Accounts
        for(Account a : Oldacc){
            a.Total_Number_of_Contacts__c=OldCon.size();
            
        }update Oldacc;
        }
    }
}

 

 

KrishHariKrishHari

Are you sure if the trigger is not firing or just if your condition is not met? I suggest you to put at least two System.debug statements - one as your very first statement in the trigger code and another inside the check if(Trigger.isUpdate) code block.

 

Regards,

HK.

justin_sfdcjustin_sfdc

Hi, 

Would it not work if you create a rollup summary field to get the count of the contacts associated with the account and update that field in account with a workflow.

 

Otherwise, what you could do is: 

Instead of;

List<Contact> con = [select id from contact where AccountId in :aId];

do this;

 Integer numOfContacts= [select id from contact where AccountId in :aId].size();

and then finally,

for (Account a: acc) {

    a.Total_Number_of_contacts__c=numOfContacts;

}

update acc;

 

 

Thanks,

Justin~sfdc

HobieHobie

Cannot do a rollup summary in this instance.  Just not available.

 

I tried your suggestion and still updating a contact record does not update the Total Number of Contacts field on the Account Page.  Everything else works great!

Tim BarsottiTim Barsotti

You need to ensure the map sizes you are matches match. This will not hold up against batch operations of more than 1 contact / account update at a time. This will break using dataloader. Before you match the size of a list to an account, you need to make sure all of the account IDs match the Contacts.AccountIds. 

 

Your code might be impacted as there are missing brackets around your if statement. 

 

if(opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c) {
              aId.add(opp.AccountId);
              OldAId.add(Trigger.oldMap.get(opp.id).AccountId);

      }

 

 

justin_sfdcjustin_sfdc
Hi Hobie,
Not sure if this will work but why dont you try and put the update statement along with the insert;
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate){

I think it should work.
Let me know! :)

Justin~sfdc