Thursday, June 26, 2014

How to use "Savepoint" and "Rollback" in apex Salesforce

Many times when we are coding and handling exception we do not thing about transaction control. Transaction control is a process to handle the flow to data that is being committed to data base based on the condition that satisfy our logic. Consider this in a code flow you are updating Account and at the same time you are creating related Opportunity records, this is in try catch block and the yet to be created Opportunity records do not satisfy the validation condition and fails to insert, but the Accounts are already updated and because it is a try block the error is just thrown to catch which do not do any thing.


We can use "Savepoint" and "Rollback" to revert the changes that "Update" operation has done.

Example:
In the above code we are setting "Savepoint" at line 9 and reverting database at line 17.
Hope this help some one.

No comments:

Post a Comment