1

I've written this trigger to Update Total Numbers of Contact in related Account Custom Field... All Code is written in Apex Trigger However, I've been told that it's best practice to split this into an Apex Class with a simple Trigger calling the Class Can someone please guide me How do I add apex Class with trigger on below code? I know it is too much to ask but it will clear my concept related apex:Class and in Future i'll be able to create Neat and Clean Trigger.

trigger UpdateContactCountOnAccount on Contact (after insert, after update, after delete)
{
    Set<Id> accountsIds = new Set<Id>();
    List<Account> acc = [SELECT Id,No_of_Contacts__c FROM Account WHERE Id IN:accountsIds];
    List<Contact> con = [SELECT Id FROM contact WHERE AccountId IN :accountsIds];
    if(Trigger.isInsert)
    {
        for(Contact contactTriggerList : Trigger.New)
        {
            accountsIds.add(contactTriggerList.AccountId);
        }
        for(Account accountList : acc)
        {
            accountList.No_of_Contacts__c=con.size();
        }
        Update acc;
    }
    if(Trigger.isDelete)
    {
        for(Contact contactTriggerList : Trigger.old)
        {
            accountsIds.add(contactTriggerList.AccountId);
        }
        for(Account accountList : acc)
        {
            accountList.No_of_Contacts__c=con.size();
        }
        Update acc;
    }
    if(Trigger.isUpdate)
    {
        Set<Id> oldAccountIds = new Set<Id>();
        for(Contact contactTriggerList : Trigger.new)
        {  
            if(contactTriggerList.AccountId != Trigger.oldMap.get(contactTriggerList.id).AccountId)
            {
                accountsIds.add(contactTriggerList.AccountId);
            }
            oldAccountIds.add(Trigger.oldMap.get(contactTriggerList.id).AccountId);    
        }  
        if(!accountsIds.isEmpty())
        {  
            for(Account accountList : acc)
            {
                accountList.No_of_Contacts__c=con.size();
            }
            Update acc;

            //for Old Accounts and Contacts
            List<Account> OldaccountList = [SELECT Id,No_of_Contacts__c FROM Account WHERE Id IN:oldAccountIds];
            List<Contact> OldContactList = [SELECT Id FROM contact WHERE AccountId IN :oldAccountIds];

            for(Account accountList : OldaccountList)
            {
                accountList.No_of_Contacts__c=OldContactList.size();
            }
            Update OldaccountList;
        }
    }
}

Thanks, Dave

3

The idea is to move logic in the trigger to the trigger handler. To use Trigger.NEW, Trigger.newMap and so on, just pass them as arguments into methods like:

public static void processInsert(Map<Id, Contact> newMap) {

}

Therefore the equivalent of Trigger.newMap will be just newMap, for Trigger.NEW - newMap.values().

1

I am trying like this Trigger

trigger UpdateContactCountOnAccount on Contact (after insert, after update, after delete)
{
    if(Trigger.isInsert)
    {
        UpdateContactCountOnAccountHandler.contactSizeHandler(Trigger.New);
    }
}

and Apex Class

public class UpdateContactCountOnAccountHandler {
    Set<Id> accountsIds = new Set<Id>();
    List<Account> acc = [SELECT Id,No_of_Contacts__c FROM Account WHERE Id IN:accountsIds];
    List<Contact> con = [SELECT Id FROM contact WHERE AccountId IN :accountsIds];

    public static void contactSizeHandler(list<Contact> conList){
        for(Contact contactTriggerList : conList)
        {
            accountsIds.add(contactTriggerList.AccountId);
        }
        for(Account accountList : acc)
        {
            accountList.No_of_Contacts__c=con.size();
        }
        Update acc;
    }

}

But it is throwing error 'Variable does not Exists'. Can you please check and correct me

  • 1
    I think it is because of Static Keyword – Dave Jul 18 at 14:38
  • If you're accessing an static method, you either do your queries inside your static method or declare them as static as well. – Alexander Aeons Torn Jul 18 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.