Database.setSavePoint specifies a point in the request at which the database's state is known. The database can be restored in the exact same state as it was at the moment you generated the savepoint, and any DML operations performed after that point can be ignored. We learn How to use database.setSavePoint in soql and also see example to use database.setSavePoint method.
How to use Database.setSavePoint?
DML operations can be rolled back at a point defined by Database.setSavepoint(). The application will roll back to the most recent savepoint if a DML operation with several statements encounters an error, the transaction as a whole won't be aborted.
Sample Code:
In this example, if any error occurs while inserting the Contact ‘c’, then the entire transaction will be rolled back to savepoint sp (as specified in the catch section by Database. rollback method).
Considerations to use setSavePoint
If you generate more than one savepoint, subsequent savepoint variables become invalid. For example, if you first generated a savepoint SP1, then generated a savepoint SP2, and then rolled back to SP1, the variable SP2 is no longer valid. You will get a runtime error if you try to use it.
During a rollback, static variables are not reversed. The static variables keep the initial value even if you try to run the trigger again.
A rollback does not clear the ID on a sObject that was added after a savepoint was set.
Scenario
Let's consider a use case of ‘Every Account (Parent) record has to have a Contact(child) record’. But what if, the contact record insertion causes an exception?
Let's suppose we have a validation rule on the Contact object in which the Last Name should not contain the keyword ‘last’. When you run the above code, you will get an error. We have two solutions for this problem.
Using try/catch
Using the methods of Database Class for partial commit
Our use case is ‘Every Parent record has to have a Child record’ and in any of the above approaches, the Account(parent) record will get created. So the above solutions won’t work, because when you use any of the above approaches, the system will not be responsible for rollbacks. You need to handle it explicitly.
Apex gives you the ability to create a point called savepoint that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, and the database can be restored to the same condition it was in at the time you generated the savepoint.
Key- Points
Each savepoint you set and rollback count against the governor limit for DML statements
Static variables are not reverted during a rollback.
if you generated savepoint SP1 first, savepoint SP2 after that, and then rolled back to SP1, the variable SP2 would no longer be valid. You will receive a runtime error if you try to use it.
The ID of the inserted sObject is not cleared after a rollback after setting a savepoint.
Each savepoint you set counts against the governor limit for DML statements.
If you want to learn about DML and Database methods in detail, you can refer to this blog. https://www.avenoir.ai/post/apex-database-dml
Happy!! to help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
References
Blog Credit:
A. Kumar
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai