+ Start a Discussion
Justin Cairns 14Justin Cairns 14 

How can I have a delete trigger a recalculation?

I am creating a trigger that will call code to count contacts at the account level meeting certain criteria.  It's working for Adds and Updates, but when I delete a contact the trigger is not updating anything.  Here is the trigger.  Do I need to place the isDelete somewhere else so that it will use the current values instead of the values before the action?  

trigger TAFS_Contact_Trigger on Contact(after insert, after update, after delete){
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
        }
        if(Trigger.isDelete){
            TAFS_ContactTriggerHandler.contactInsert(Trigger.old);
        }
        if(Trigger.isUpdate){
            TAFS_ContactTriggerHandler.contactInsert(Trigger.new);
        }
    }
Tejpal KumawatTejpal Kumawat
Hi Justln,

Check you class "TAFS_ContactTriggerHandler" in which you are querying or not for number of contact for a particular account. Id not then get contacts related Account & then update count field.

Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
Justin Cairns 14Justin Cairns 14
Here's s the class.  Would I make the change in the // update the number of owner contacts in Account    section? 


public static void contactInsert(List<Contact> contactList){
        Set<Id> leadSetIds =  new Set<Id>();
        
        // Added for extending the trigger to Client contacts
        Set<Id> clientSetIds =  new Set<Id>();
        
        List<Contact> contacts = [select id,TAFS_Contact_Type__c,TAFS_Lead__r.id,Account.id, Account.name from Contact where id in :contactList];
        
        for(Contact contactObj : contacts){
            // Getting Leads Ids for new Lead contacts
                if(String.IsNotBlank(contactObj.TAFS_Lead__c) && (contactObj.Account.name == 'TAFS Inc.' || contactObj.Account.name == '')){
                    leadSetIds.add(contactObj.TAFS_Lead__c);
                    system.debug(contactObj+',1 lead id: '+contactObj.TAFS_Lead__c);     
                }
                
                // Getting Client Ids for new Client contacts
                if(contactObj.Account!=null && contactObj.Account.name != 'TAFS Inc.' ){
                    clientSetIds.add(contactObj.Account.id);
                }
         }
       
        system.debug(contactList[0].id+', lead id: '+contactList[0].TAFS_Lead__c);
        // update the number of owner contacts in Leads       
        List<Lead> leadList = [select Id,TAFS_Number_of_Contacts__c,TAFS_General_Manager_tag__c,(SELECT Id,name FROM Contacts__r) FROM Lead where Id IN :leadSetIds AND isConverted = False];
        for(Lead leadObj:leadList){
            leadObj.TAFS_Number_of_Contacts__c = leadObj.Contacts__r.size();
           
            //Dynamically assign last signer role to GM based on the number of owner contacts
            Integer docusignRoutingNo = leadObj.Contacts__r.size() + 1;
            leadObj.TAFS_General_Manager_tag__c = 's'+docusignRoutingNo;
        }
        Database.update(leadList);
        
        // update the number of owner contacts in Account   
        List<Account> clientList = [select Id,TAFS_Number_of_Owner_Contacts__c,TAFS_General_Manager_tag__c,(SELECT Id,name FROM Contacts WHERE TAFS_Contact_Type__c = 'Owner') FROM Account where Id IN :clientSetIds];
        for(Account clientObj : clientList){
           clientObj.TAFS_Number_of_Owner_Contacts__c = clientObj.Contacts.size();
            
            //Dynamically assign last signer role to GM based on the number of owner contacts
            Integer docusignRoutingNo = clientObj.Contacts.size() + 1;
            clientObj.TAFS_General_Manager_tag__c = 's'+docusignRoutingNo;
         }
         try{
            Database.update(clientList);
        }
        catch(Exception ex){
            for(Contact clientObj : contactList){
                clientObj.addError(Label.TAFS_Contact_Validation_Message);
            }
        }
        
        
    }
}
Tejpal KumawatTejpal Kumawat
Hi Justln,

Your code look like fine, Just check conditions those you are testing matches with trigger condtions.

Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
Justin Cairns 14Justin Cairns 14
They seem to match.  It just doesn't update the count when I delete an existing contact.  I'm guessing becuse the delete trigger is using the OLD contact list.  I'm not sure how to get a delete to trigger where it will use the new contact list after the contact causing the trigger is deleted.  Adds and Updates work fine.  Deletes don't cause the count to change.  Not sure what I'm doing wrong.  
Tejpal KumawatTejpal Kumawat
There are some conditions like Account.name != 'TAFS Inc.' and TAFS_Contact_Type__c = 'Owner', If you match these criteria then it will change count. Otherwise it will remain same.
Justin Cairns 14Justin Cairns 14
The conditions are correct.  It's just not working for deletes.  I think it is because the delete trigger is using Trigger.old...so it's using the list of contacts prior to the delete action.  But you can't use Trigger.new on deletes.  I'm not sure how to get around this.