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
Cato1984Cato1984 

Do before insert triggers allow for partial updates?

This is a general question, I have no code examples yet. I hope I can convey my question in a manner you guys can understand without examples, let me know if I need to clarify.

 

I'm reading up on Apex, learning what I can do while I think through my solution. I will have two custom objects, one parent one child, so a one-to-many relationship. The child will have multiple Record Types. Several of the record types will have no limit on the number of recrds that can be created for each parent. There is however one record type where I will need to prevent the creation of that type if one already exists for that specific parent. I believe I can solve the issue with a before trigger. Where I get stumped is when these records get created in bulk, e.x. creating two hundred records via the data loader. I see the Database.insert() method will allow me to do partial saves on after triggers, but there is no documentation around before triggers and partial saves, at least none that I can find. Do before triggers support partial saves natively? If not, is there some other way I can ensure partial data loads?

 

Thanks in advance,

Cato

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Triggers do allow partial saves. When a trigger executes, all records in the trigger are not committed. If an "addError" call is called on a record in the trigger (e.g. Trigger.new[x].addError), and if allOrNone is set to false, then the trigger can be retried minus the records that were flagged as errors (there is a limit of 3 retries before a general failure occurs). Generally, there is no special coding requirements that you need to take into consideration to support partial saves; the only limitation is that the trigger must not throw an exception, which will cause all 200 rows to fail.