0

The Account object has a required field called Test__c to create records in system. I have an apex trigger which updates the field called Copy_Name__c with same details as Account Name. I was trying to run the below code through execute anonymous window and the code is failing to insert at least one record.

Is it possible to run the partial commits in before insert triggers. I tried to find any existing post in communities and I don't see anything.

trigger AccountTrigger on Account (before insert) {
    for (Account account : Trigger.new) {
        account.Copy_Name__c = account.Name;
    }
}

Code through Anonymous window:

List<Account> accounts = new List<Account> {
    new Account(
        Name = 'Test ABCD',
        Test__c = 'Hello'
    ),
    new Account(
        Name = 'Test 2345',
        Activity_date__c = System.today()
    )
};

insert accounts;

I was expecting the it may insert the first record and I am passing all details in it. Is it some limitation with anonymous windows/is there anyway to work with partial commits with before triggers?

Thanks in advance

1 Answer 1

1

When you use the insert, update, delete, etc... keywords, that uses the default mode of operation for DML.

That is to say, it's all or none. If one record runs into an error, the entire DML operation fails. That's not just an anonymous apex thing, that's how DML behaves everywhere.

If you want to allow partial success, then you should use the DML methods in the Database class. E.g. Database.insert(accounts, false);

The second argument controls the "allOrNone" parameter, and setting it to false is what allows partial success.

When using that, you should generally store and iterate over the List<Database.SaveResult> you get as a result to find out which records failed (and which succeeded). There are some odd things to keep in mind when using allOrNone=false DML operations:

  • On a failure within a single trigger chunk, Salesforce detects the failure and re-tries the chunk without the failed records. This happens up to 3 times.
  • Static variables are not reset when Salesforce retries a chunk. This can lead to odd behavior that's hard to reproduce/debug (but if you remember this quirk, you can at least fairly easily pin it down to being related to allOrNone=false).
  • Specifically for inserts, a partial failure will cause the record Ids for the successful records to be tossed out. They'll get entirely new Ids on the next retry. This is usually not a problem, and it's mostly transparent to us (unless you store the record ids in a static variable)
3
  • Perfect. Thanks Derek. It means no changes requires on apex trigger as it is before Insert code. Is that correct?
    – KSL
    Commented Nov 19, 2023 at 7:56
  • @KSL Yes, that's correct. Also, the documentation makes mention of the retry mechanism not working if you provide an invalid value for a field, but I don't know precisely what situation that is trying to describe.
    – Derek F
    Commented Nov 19, 2023 at 13:32
  • I think it worth mentioning that the trigger author needs to allow for partial success by not throwing exceptions and instead marking records or record fields with errors using the appropriate SObject methods.
    – Phil W
    Commented Nov 19, 2023 at 14:28

You must log in to answer this question.

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