You need to sign in to do that
Don't have an account?
miha198206
DML commit when errors in trigger
I have a trigger. In this trigger I have a line:
contact.addError( 'Candidate \'' + contactQuery + '\' is already exists. This candidate can\'t be inserted. Existed candidate was updated');
After this line of code I put other line of code to update other record. Something like:
otherContact.Name = 'Test Name'; update otherContact;
Since I have an 'addError' statement in this trigger my 'update otherContact' statement is terminated. How can I update record in this case?
addError
Hi Miha,
Using addError will prevent any changes (and will roll-back if after insert).
Unfortunately there is no way to simply add a warning within salesforce yet.
I am facing the same problem too.
The trigger that me and my manager use also has a Trigger.error to notify user when an exception occurs.
We are simultaneously trying to save the exceptions to a custom object as well as create a workflow to send out the exceptions to Customer Service Reps.
The Problem : On Save or update the trigger.adderror message makes the whole transaction roll back since the Update/Insert and Error message are done in the same thread.
We even tried having a future method to save the Exceptions,but the DML Insert did not happen.
My Conclusion: We cannot have "Error" as well as "Commit" together.
I looked up a documentation which spoke about partial Processing.
I am not too sure how to handle this problem.
All that I am looking for is a Message saying"Exceptions Occurred!!! Please Check Exception Log Object" and save the exception simulateously to a custom object.
Please throw some light on how to Solve this issue :(
You can't do this.Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field.
Use Database Method Syntax
SaveResult Database.insert(sObject recordToInsert, Boolean opt_allOrNone | database.DMLOptionsopt_DMLOptions)
SaveResult[] Database.insert(sObject[] recordsToInsert, Boolean opt_allOrNone | database.DMLOptionsopt_DMLOptions)
How this methods can help? Do you have any examples?
Refer Apex Doc
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_bulk_sharing_creating_with_apex.htm