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
Abhishek Sharma 527Abhishek Sharma 527 

exception handling in trigger

Hello there, I have created custom roll up summary and now I was asked to add exception handling in that, as it's properly working code, I'm not sure where to put these try - catch block
can anyone plz help in this.

// my code
public class Contact_count_Handler 
{     
    public static void InsertMethod(List<Contact> newList){
        List<Account> accList = new List<Account>();
        
        Set<id> accountIds = new Set<id>();        
        if(newList!=null) { 
            for(contact c: newList)    
            {
                if(c.accountId!=null)            
                {
                    accountIds.add(c.accountId);
                }
            }
            if(!accountIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){            
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    update accList; 
                }
            }
        }
        
    }           
    
    public static Void updateMethod(list<Contact> newList,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();
        
        if(oldmap != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
                    setAccIds.add(con.AccountId); 
                    setAccIds.add(oldMap.get(con.Id).AccountId);
                }   
            }
            if(!setAccIds.isEmpty()){
                
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    update accList; 
                }
            }
        }
    }
    
    
    public static Void deleteMethod(list<Contact> newList){
        List<Account> accList=new List<Account>();
        
        if(newList != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null){
                    setAccIds.add(con.AccountId);      
                }   
            }
            
            if(!SetAccIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    update accList; 
                }
            }
        }
    }
}
Best Answer chosen by Abhishek Sharma 527
mukesh guptamukesh gupta
Hi Abhishek,

Please use below code:-
 
public class Contact_count_Handler 
{     
    public static void InsertMethod(List<Contact> newList){
        List<Account> accList = new List<Account>();
        
        Set<id> accountIds = new Set<id>();        
        if(newList!=null) { 
            for(contact c: newList)    
            {
                if(c.accountId!=null)            
                {
                    accountIds.add(c.accountId);
                }
            }
            if(!accountIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){            
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                 try{
                    update accList; 
                    }catch(DMLException e){
                 
                 System.debug('The following exception has occurred: ' + e.getMessage()); 
                  }
                   
                }
            }
        }
        
    }           
    
    public static Void updateMethod(list<Contact> newList,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();
        
        if(oldmap != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
                    setAccIds.add(con.AccountId); 
                    setAccIds.add(oldMap.get(con.Id).AccountId);
                }   
            }
            if(!setAccIds.isEmpty()){
                
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                   try{
                        update accList; 
                      }catch(DMLException e){
                       System.debug('The following exception has occurred: ' + e.getMessage());
                   }
                }
            }
        }
    }
    
    
    public static Void deleteMethod(list<Contact> newList){
        List<Account> accList=new List<Account>();
        
        if(newList != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null){
                    setAccIds.add(con.AccountId);      
                }   
            }
            
            if(!SetAccIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    try{
                     update accList; 
                    }catch(DMLException e){
                    System.debug('The following exception has occurred: ' + e.getMessage());
                    }                
                }
            }
        }
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

You have to write all the code in try block and add the catch block as below. I am writing for single method. 
 
public static void InsertMethod(List<Contact> newList){
        List<Account> accList = new List<Account>();
        try{
        Set<id> accountIds = new Set<id>();        
        if(newList!=null) { 
            for(contact c: newList)    
            {
                if(c.accountId!=null)            
                {
                    accountIds.add(c.accountId);
                }
            }
            if(!accountIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){            
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    update accList; 
                }
}
Catch(exception e) {
    }            }
        }
        
    }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
mukesh guptamukesh gupta
Hi Abhishek,

Please use below code:-
 
public class Contact_count_Handler 
{     
    public static void InsertMethod(List<Contact> newList){
        List<Account> accList = new List<Account>();
        
        Set<id> accountIds = new Set<id>();        
        if(newList!=null) { 
            for(contact c: newList)    
            {
                if(c.accountId!=null)            
                {
                    accountIds.add(c.accountId);
                }
            }
            if(!accountIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){            
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                 try{
                    update accList; 
                    }catch(DMLException e){
                 
                 System.debug('The following exception has occurred: ' + e.getMessage()); 
                  }
                   
                }
            }
        }
        
    }           
    
    public static Void updateMethod(list<Contact> newList,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();
        
        if(oldmap != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
                    setAccIds.add(con.AccountId); 
                    setAccIds.add(oldMap.get(con.Id).AccountId);
                }   
            }
            if(!setAccIds.isEmpty()){
                
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                   try{
                        update accList; 
                      }catch(DMLException e){
                       System.debug('The following exception has occurred: ' + e.getMessage());
                   }
                }
            }
        }
    }
    
    
    public static Void deleteMethod(list<Contact> newList){
        List<Account> accList=new List<Account>();
        
        if(newList != null){
            Set<Id> setAccIds = new Set<Id>();
            for(Contact con : newList){ 
                if(con.AccountId != null){
                    setAccIds.add(con.AccountId);      
                }   
            }
            
            if(!SetAccIds.isEmpty()){
                for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
                    
                    acc.Number_of_linked_contact__c= acc.contacts.size();
                    
                    acclist.add(acc);
                }
                if(acclist.size()>0){
                    try{
                     update accList; 
                    }catch(DMLException e){
                    System.debug('The following exception has occurred: ' + e.getMessage());
                    }                
                }
            }
        }
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer