You need to sign in to do that
Don't have an account?

Catch integration errors within salesforce triggers
I am trying to insert a record from developer console for object 'TestTriggers__c'. The record is failing to insert as expected because I am not passing the required field 'Amount__c'. I need to, however, catch this in my trigger, which i am unable to. I can see the error 'System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Amount__c]: [Amount__c]' in the debug log, but unable to catch it within my trigger 'TestTriggers'. Is there any way to do that. I need to use the same approach to catch the errors from different integrations (like data loader) we have in our SF instance.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
In your use case, you want to capture the exceptions and want to manipulate and insert log in an object.
In such case, If you are inserting in apex, you can use try- catch block to capture the exception and read the error message and you can manipulate as required.
In trigger, you cannot capture the exception in apex, where DML is invoked from somewhere else.
Approach 1:
In the dataloader, you get the success log and error log.
Insert the Error Log (output) to Some Target Table (Error Log - Custom Object in Salesforce) manually.
Approach 2:
Write Java application to read the data from CSV and insert to particular table.
In that insert, you can write try- catch block to capture the exception , if you get the exception, insert in Log table in salesforce
Approach 3:
In the Apex Trigger, You have to manually check all the mandatory fields/Validation rule in Before Trigger event. If its null or empty, Insert Log. then skip validation and mandatory by giving proper data, and mark it as to be deleted.
Run a daily scheduler to delete it.
Thanks
I do have a workaround -
Make deep clone copy trigger.new list of records in before insert/update context.
In try/catch, create save point -> insert new deep clone list -> if there is an error, it will enter catch, then create error log -> if success, use rollback to previous save point to rollback the deep clone list insert.
You can automate loading error log to insert log record using CLIQ Dataloader.
https://salesforceidiot.blogspot.com/2023/05/error-logging-framework-in-salesforce.html