You need to sign in to do that
Don't have an account?
Ravi 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.
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.
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.
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. Best Regards,
Anthony McDougald
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.