1

Example: If we are inserting 10 records in an object, Where 5 records are correct and the remaining 5 records are incorrect. • In Database.insert 5 records will be inserted, remaining 5 records will be failed.(i.e. Partial DML Operation). (record.addError is in after insert trigger.) (no workflows, flows, or process builder on that object or validation rules)

Assumption: database.insert goes first time through trigger and in the end of after insert trigger we can do soql and see 10 records ids and etc.(they are not committed yet) then before commit to DB occurs error and system starts again insertion(now only of the correct records) and in trigger goes these 5 correct records(so it goes to trigger the second time) and process continues till the end.

Question: tell me please is that correct? (before answer check please :) ).

0

1 Answer 1

3

It could even run thrice.

When errors occur because of a bulk DML call that originates from the SOAP API with default settings, or if the allOrNone parameter of a Database DML method was specified as false, the runtime engine attempts at least a partial save: During the first attempt, the runtime engine processes all records. Any record that generates an error due to issues such as validation rules or unique index violations is set aside.

If there were errors during the first attempt, the runtime engine makes a second attempt that includes only those records that did not generate errors. All records that didn't generate an error during the first attempt are processed, and if any record generates an error (perhaps because of race conditions) it is also set aside.

If there were additional errors during the second attempt, the runtime engine makes a third and final attempt which includes only those records that didn’t generate errors during the first and second attempts. If any record generates an error, the entire operation fails with the error message, “Too many batch retries in the presence of Apex triggers and partial failures.

Apex triggers are fired for the first save attempt, and if errors are encountered for some records and subsequent attempts are made to save the subset of successful records, triggers are re-fired on this subset of records.

Reference

Not the answer you're looking for? Browse other questions tagged .