Consider I have a batch class which updates 50 records of Task.

The batch class ran successfully and updated the Task records.Now the Task object has a trigger that runs on after/before update. Due to some issue the trigger throws an exception.

Will only the trigger update will fail or will it rollback the entire update including the one's done by the batch as it is a part of single transaction?


The entire update will fail. From Apex Developer Guide - Apex Transactions:

All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, a Visualforce page, or a custom Web service method.

If your code starts in a batch which then fires a trigger, it's treated as one transaction. It's exactly the same as updating a record from the UI which causes a trigger to fail - an error will be presented to your screen. If your batch fails, you can see what was the reason in Setup->Environments->Jobs->Apex jobs in the Status Detail column.


It mostly depends from how are you performing your database operations. By DML language or Database Apex Class? If you want to partially commit records to the database with omitting these which cause the trigger to fail, use methods from a Database Class like insert(recordsToInsert, allOrNone):

The optional 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. This method returns a result object that can be used to verify which records succeeded, which failed, and why. If the parameter is not set or is set true, an exception is thrown if the method is not successful.


In the case of an exception in a trigger, all records involved in that trigger will not be updated, regardless of if allOrNone is set, because Salesforce cannot determine which records were successful. Triggers are synchronous in nature. The "before update" and "after update" simply refers to the half-way point initial commit phase. This initial commit is not permanent, and can be rolled back by an error up until the point where the transaction is finalized and post-transaction logic kicks in (sending emails, etc).

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.