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
Ravi Sankar 12Ravi Sankar 12 

try catch block in triggers

I have a scenario where I need to create records in a custom object whenever an exceptioin occurs(Dml,email,async or any other types of exceptions). I have written a gerneric apex class to create records. Whenever exception occurs, in the catch block I am calling a method to create a excetion record.
Below is my code. I am thinking of a workaround to bulkify the below code.
public static void testDMLUpdate(Test_CDI__c[] newData){
        for(Test_CDI__c T: newData){
            
            if(T.Debug_Field__c == null){
                Test_CDI_2__c test = new Test_CDI_2__c();
                test.Name = 'Debug filed is null';
                try{
                	insert test;
                    system.debug('inserting the record no exctption fired');
                }
                catch(exception e){
                    CDI_MessageLogger.logException(e, T.Id);
                }
            }
        }
    }

I know writing a dml operation in the for loop is bad. but I need to capture the Id of the record causing the exception.  CDI_MessageLogger.logException(e, T.Id); is the method creating record in the custom object for each exception.

Thanks in advance.
Anthony McDougaldAnthony McDougald
Hello Ravi,
Hope that your day is off to an amazing start. A great way to bulkify your code would be to create a list before using an interation loop, having fields in the object of the list that will hold the said information (maybe even a lookup field), add the new records to the list in your catch block, then insert all of the records at the end of the operation. Below is an example of what we're discussing and please feel free to ask any questions if you get stuck. Have a blessed day.
public static void testDMLUpdate(Test_CDI__c[] newData){
//Create a list of a custom sObject that will hold the information of your exceptions
List <Custom_Exception_Object__c> holder = new List<Custom_Exception_Object__c>();
        for(Test_CDI__c T: newData){
            
            if(T.Debug_Field__c == null){
                Test_CDI_2__c test = new Test_CDI_2__c();
                test.Name = 'Debug filed is null';
                try{
                	insert test;
                    system.debug('inserting the record no exctption fired');
                }
                catch(exception e){
//Create a new record of this custom object and store the Id of the bad record in a record of that custom object
                    Custom_Exception_Object__c badRecords = new Custom_Exception_Object__c (Custom_field__c = T.Id);
//Add the record to your list
holder.add(badRecords);
                }
            }
        }
//Insert all the records in to your database at once with one DML operation, Yay! :D
insert holder;
    }
Best Regards,
Anthony McDougald
 
Ravi Sankar 12Ravi Sankar 12
Hello Anthony,

Thank you for your time. 
We still have a insert statement in for loop inside the try block. Will that be a problem?

Thank you.