I'm throwing error before insert when there is duplicate email and performing some action, but when error is thrown all the operation i performed is rolled back. how to prevent rollback.
3 Answers
The rollback depends on how the DML was initiated.
Insert, update, delete, etc is an all or none and rollback will occur
Using database methods you can specify the behavior using the allOrNone option
Database.insert(records, false);
Will allow records without error to be inserted and those with an error will not.
You can inspect the results using the SaveResult object.
for inserting bulk record use
database.Insert
It will not rollback all the records it will only add error to specific record.
Salesforce doesn't provide commit
statement so you could save the data prior to that point and then throw the error.
When you throw any custom error or add an error to a record in Trigger, Salesforce will rollback the transaction. You you cannot change that behavior.
-
You can't save and then throw an error. It's not possible. And SF provides a means to alter the all or none behavior– EricCommented Aug 13, 2017 at 6:23