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
bvdbbvdb 

Preventing an Insert with trigger

Is there a way to prevent a record from being insert in with (Trigger.isBefore && Trigger.isInsert)?

 

For example, if I insert 5 records and one of them isn't valid (based on a custom validation I have in the trigger), can I have the system bypass the invalid record and only insert the other 4?

 

I'm using addError() in my trigger for invalid records. This works fine in the UI but when I insert a list of records, the entire insert fails if it comes across one erroneous record.

bob_buzzardbob_buzzard

If you use the Database.insert method, then according to the docs:

 

The optional opt_allOrNone parameter specifies whether the operation allows partial success. If you specify false for this

parameter and a record fails, the remainder of the DML operation can still succeed