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
miha198206miha198206 

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
Craig_XactiumCraig_Xactium

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.

 

 

prathap raoprathap rao

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 :(

 

Pradeep_NavatarPradeep_Navatar

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.

NaishadhNaishadh

Use Database Method Syntax

 

• SaveResult Database.insert(sObject recordToInsert, Boolean opt_allOrNone | database.DMLOptions
opt_DMLOptions)
• SaveResult[] Database.insert(sObject[] recordsToInsert, Boolean opt_allOrNone | database.DMLOptions
opt_DMLOptions)

 

SaveResult Database.insert(sObject recordToInsert, Boolean opt_allOrNone | database.DMLOptionsopt_DMLOptions)

SaveResult[] Database.insert(sObject[] recordsToInsert, Boolean opt_allOrNone | database.DMLOptionsopt_DMLOptions)

miha198206miha198206

How this methods can help? Do you have any examples?