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
CBNCBN 

How to write DML Statements in try catch block for below Code

Hi all,

How to Write DML Statements in try catch block 

// Handler Class
 
public class CountContactHandler
{
                public static void countContacts(List<Contact> conList){
                                Set<Id> setAccountIds = new Set<Id>();
                                for(Contact con : conList)
                                {
                                                setAccountIds.add(con.AccountId);
                                }
                               
                                List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
                               
                                for(Account acc :listAccs)
                                {
                                                Integer i=0,j = 0;
                                                for(Contact conItr : acc.Contacts){
                                                                if(conItr.Contact_Roll__c)
                {
                    i++;
                }
                                                                j++;
                                               
                                                }
                                                acc.No_of_active_contacts__c = i;
                                                acc.Total_No_Of_Contacts__c = j;
                                }
                                update listAccs;
                }
}
// Trigger
 
trigger countContact on Contact (after insert, after update, after delete, after undelete)
{
                Set<Id> setAccountIds = new Set<Id>();
 
                //Whenever your working with After Undelete operation you can access data through
                //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
                if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
                {
                                CountContactHandler.countContacts(Trigger.new);
                }
 
                else if(Trigger.isDelete)
                {
                                //if you use Trigger.new below in place of Trigger.old you will end up with
                                //System.NullPointerException:Attempt to de-reference a null object
                                CountContactHandler.countContacts(Trigger.old);
                }
}

Kindly support and suggest

Thanks
 
Best Answer chosen by CBN
Ajay K DubediAjay K Dubedi
Hi CBN,

public class Yaprofile {
    public static void countContacts(List<Contact> conList){
        try{
            Set<Id> setAccountIds = new Set<Id>();
            for(Contact con : conList)
            {
                setAccountIds.add(con.AccountId);
            }
            
            List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
            
            for(Account acc :listAccs)
            {
                Integer i=0,j = 0;
                for(Contact conItr : acc.Contacts){
                    if(conItr.Contact_Roll__c)
                    {
                        i++;
                    }
                    j++;
                    
                }
                acc.No_of_active_contacts__c = i;
                acc.Total_No_Of_Contacts__c = j;
            }
            update listAccs;
        }
    }catch(exception ex){
        system.debug('exception'+ex.getLineNumber()+ex.getMessage());
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thanks,
Ajay Dubedi