+ Start a Discussion
Sidharth Agarwal 8Sidharth Agarwal 8 

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.
Sidharth Agarwal 8Sidharth Agarwal 8
Simple use case - I need to create error logs in SF for records coming from outside (ex data loader) and are failed to insert in sf (ex validation rule, missing required fields etc).
Saravana Bharathi 1Saravana Bharathi 1
Consider Trigger order of Execution in Salesforce
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
Sidharth Agarwal 8Sidharth Agarwal 8
Thanks Saravana for the information.  Approach 1 and 2 doesn't fit my requirements as our integrations could be like form assembly which directly push data to SF. Approach 3 could be cumbersome and inefficient and VR, Field Validations etc are continuously evolving. 

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.
Sidharth Agarwal 8Sidharth Agarwal 8
The above savepoint approach is not working :( Because the transaction will eventually fail because of system validation errors, and the error logs will rollback as well. Tried creating error logs in future methods, but that doesn't work too because the future call is not firing as transaction is fa\i,ing.
Saravana Bharathi 1Saravana Bharathi 1
In the approach 1,

You can automate loading error log to insert log record using CLIQ Dataloader.