When to use save point and rollback in Salesforce?
Suppose you have written long code which contains many more DML statement, at the some point you will want this DML statement should not be executed, you may need to modify your code, at that time you will have to return that point, savepoint will identifies that point , Rollback will restore the database to that point (savepoint).
Do you need to use save point and roll back?
If this post is your solution, kindly mark this as the solution and give Kudos. Savepoint and roll back will help us to create your own transcation.
How to set a save point before a DML?
The syntax is to set the save point before any dml and then rollback to that savepoint when you face any error. Would this happen automatically since you are inserting both parent and child in the same transaction. So if there is a failure, the previous successful dml should automatically rollback, right?
Why do I need to roll back data after DML operation?
This is one reason why, even if you modify data in a variable, you need to query the data back to validate the values after a DML operation. In your code, the data is rolled back, but your “copy” is now out of sync with the database.
What are the operations of DML in apex?
DML Operations. Using DML, you can insert new records and commit them to the database. You can also update the field values of existing records. Inserting and Updating Records. Using DML, you can insert new records and commit them to the database. Similarly, you can update the field values of existing records.
Why do you need to query data after a DML operation?
This is one reason why, even if you modify data in a variable, you need to query the data back to validate the values after a DML operation. In your code, the data is rolled back, but your “copy” is now out of sync with the database. To verify that the data is correct, you now have to query the records again: