3

The addError method stops the DML operation and rolls back the associated changes. But I have noticed that the addError method itself doesn't stop the trigger execution and the reasoning I have heard behind this is that - we have the scenario of partial success and one error should not stop the other records from being inserted. This is indeed sensible.

Other than trimmimg out bad records explicitly by code, is there any way we can prevent the trigger from considering such addErrorED records in trigger.new for future processing. Is it possible to remove such records from trigger.new itself.

Thanks in advance.

0

You cannot "remove them` from the trigger context.

The process of failing all records or just the ones that error and letting the other be committed all depends on the process that inserted/updated/deleted the records.

For example:

update sObject; //This is an AllOrNone
database.update(sObject); //this is an AllOrNone
database.update(sObject,false); // this is NOT allornone

If it is AllOrNone, a single record failing will cause all to fail. If not AllOrNone only the records that failed will fail and the rest will be committed.

Take a loot at what code is doing the DML and you will be able to fix it there. The whole point of being able to Not use AllOrNone is so you can commit the good data AND obtain the reasons for each records failure. With AllOrNone, you only get the first error....

4
  • agreed, but do we have a mechanism to make the system stop processing those records which were flagged down by the addError method. I do not understand as to why the trigger still process those records even though it is evident that all the changes associated with them will be rolled back.
    – Prajith
    Dec 15 '15 at 10:08
  • Specifically in my case, in a custom object's trigger - I use addError and flag out those records in which a particular lookup field is not populated. Now in subsequent methods I want to access the lookup parent, the trigger even considered all the flagged down records and I ran into nullpointer exception. Explicitly filtered out the bad records before feeding it to the next method, which again is adding addError to some records. so that is more filtering in future.
    – Prajith
    Dec 15 '15 at 10:16
  • Whenever I use add error I put in a return statement. I saw the same behavior. Add error is not like throwing and error. It tells the system to throw and error at the end it seems
    – Eric
    Dec 15 '15 at 15:44
  • 1
    An addError at a later stage overrides the earlier error message too.
    – Prajith
    Dec 15 '15 at 20:11
-2

addError method prevents DML operations for all records in the given context. there is no concept of partial success with this method. trigger takes records in batches and if there is error with one record then complete transaction get rolled back.

4
  • 4
    This is simply not correct
    – Eric
    Dec 15 '15 at 6:18
  • Hi Eric, I will check this again but I believe, in trigger context adderror will not commit any records. However, in apex class or other DML ,we can use Database.insert(recordToInsert, false). I have checked this before trigger, If there is error in one record. data loader will not insert the complete batch. I have answered this in context of trigger. Dec 15 '15 at 6:22
  • You are mistaken...If a data load rejects the batch it is because the batch was set to all or none not because of the addError.
    – Eric
    Dec 15 '15 at 6:33
  • @amidstCloud the addError affects only individual records, the other records are allowed to pass.
    – Prajith
    Dec 15 '15 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.