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
Suman KoleySuman Koley 

writing a handler class of a trigger

I have written a trigger to count the number of contacts in accounts. Please help me with writing the handler class of it. I have very less knowledge on handler class:
trigger tocountcontactrecords on Contact (after insert) {

set<ID> setID = new set<ID>();
List<Account> ListAccount = new List<Account>();

for(contact con : trigger.new)

{
setID.add(con.AccountID);
}
For(account acc:[select id,name,Numberofcount__c,(select id from contacts) from account where id=:setID])

{

Account Ac1 = new Account();
ac1.Id=acc.Id;
ac1.Numberofcount__c = acc.contacts.size();
Listaccount.add(ac1);

}

update Listaccount;


}
Best Answer chosen by Suman Koley
Raj VakatiRaj Vakati
Some think like below 
public with sharing class contactHandlerCls {


 public static void contactHandler(List<contact>  con)    {
 

set<ID> setID = new set<ID>();
List<Account> ListAccount = new List<Account>();

for(contact con : con)

{
setID.add(con.AccountID);
}
For(account acc:[select id,name,Numberofcount__c,(select id from contacts) from account where id=:setID])

{

Account Ac1 = new Account();
ac1.Id=acc.Id;
ac1.Numberofcount__c = acc.contacts.size();
Listaccount.add(ac1);

}

update Listaccount;


}   
}      



invoke from the trigger 

 
contactHandlerCls.contactHandler(Trigger.new) ;

 

All Answers

Raj VakatiRaj Vakati
Some think like below 
public with sharing class contactHandlerCls {


 public static void contactHandler(List<contact>  con)    {
 

set<ID> setID = new set<ID>();
List<Account> ListAccount = new List<Account>();

for(contact con : con)

{
setID.add(con.AccountID);
}
For(account acc:[select id,name,Numberofcount__c,(select id from contacts) from account where id=:setID])

{

Account Ac1 = new Account();
ac1.Id=acc.Id;
ac1.Numberofcount__c = acc.contacts.size();
Listaccount.add(ac1);

}

update Listaccount;


}   
}      



invoke from the trigger 

 
contactHandlerCls.contactHandler(Trigger.new) ;

 
This was selected as the best answer
Vinod AgrawalVinod Agrawal
Here is what you need. In order to do it correctly, you also need to consider when the contact's Account  changes to another Account or the contact gets deleted. Hope this helps.

trigger transferContact on Contact (after insert, after update, after delete) {
    set<ID> accountIds = new Set<Id>();
    if(Trigger.isInsert){
        for(Contact c : Trigger.New)
            accountIds.add(c.accountId);
    }
    if(Trigger.isUpdate){
        for(Contact c : Trigger.New){
            if(c.AccountId != Trigger.oldMap.get(c.Id).AccountId){
                accountIds.add(c.AccountId);
                accountIds.add(Trigger.oldMap.get(c.Id).AccountId);
            }
        }
        
    }
    if(Trigger.isDelete){
        for(Contact c : Trigger.old){
            accountIds.add(c.AccountId);
        }
    }
    System.debug('Account Ids are ' + accountIds);
    HelperClass.countContacts(accountIds);
}

public class HelperClass {
    public static void countContacts(set<ID> actIds){
        List<Account> actList = [Select Id, (Select Id from Contacts), Shell__NumberOfContacts__c from Account where Id IN :actIds and isDeleted = false];
        System.debug('Account Ids are ' + actList);
        for(Account a : actList){
            a.Shell__NumberOfContacts__c = a.Contacts.size();
        }
        
        update actList;
    }
}
Ajay K DubediAjay K Dubedi
Hi Suman,

Use the following code:

Trigger:

trigger ContactCountTrigger on Contact (After insert,after update) {

  Set<id> setAccountId=new Set<id>();
   if(Trigger.isInsert ||  Trigger.isUpdate){
    for(Contact con:Trigger.New){
  setAccountId.add(con.AccountId);

  }
  }
Trigger Handler:

List<Account> listAcc=[select id,noOfContact__c,(Select id from Contacts)from Account where id in:setAccountId];
   for(Account acc:listAcc){
    acc.noOfContact__c=acc.contacts.size(); 
   }
   update listAcc;
}


There is a custom field I have made for Account object ie. (noOfContact), holding size of contacts present in an account.
Hope this code will solve your query please mark it as best answer if you find it helpful.

Thanks.
Ajay Dubedi
Suman KoleySuman Koley
Hi Guys,

I just found the sollucion for this one.
Trigger should be :  
 
trigger CreateNewContact on Account (after insert, after update){

    if(Trigger.isinsert){
    CreateNewContact.handlerafterinsert(trigger.new);
    
    }
    }


And the class should be :
public class CreateNewContact {
public static Boolean isrecursive = true;
public static void handlerafterinsert(list<Account> listAccount){
 

    
        List<Contact> ct = new List <Contact>();
        
        for(Account acc : listAccount){

        Contact c = new Contact(LastName = acc.name,
                        AccountId=acc.id,
                        Fax=acc.Fax,
                        MailingStreet=acc.BillingStreet,
                        MailingCity=acc.BillingCity,
                        MailingState=acc.BillingState,
                        MailingPostalCode=acc.BillingPostalCode,
                        MailingCountry=acc.BillingCountry,
                        Phone=acc.Phone);

        ct.add(c);
        
        }
        
        if(!ct.isEmpty())
            insert ct; 
    }

And it is working without any error. Thanks Vinod btw.