You need to sign in to do that
Don't have an account?
SFDC_Learner
SavePoint and Rollback
Savepoint sp = Database.setSavepoint();
Database.rollback( sp );
Can you tell me what is the advantage of above statements, plz give a small ex to understand this.
https://sites.google.com/site/infallibletechie/what-is-database-savepoint-in-salesforce3
check this...
Regards,
Magulan D
Salesforce.com certified Force.com Developer.
SFDC Blog
SFDC Site
If this post is your solution, kindly mark this as the solution and give Kudos.
HI
Savepoint and roll back will help us to create your own transcation. 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).
Here is some code you can refer
Did this post answers your question,if so please mark it solved
Thanks
Suppose, There is a scenario where you insert parent with child record from visualforce page.
Case 1 : Unfortunately there was any error in creating child record(like user didn't fill required fields), but parent has been created successfully.
case 2 : User has filled the required information and clicked on save button again. So in this case there will be two parents created.
Now if you look into data base, there will be two parent records (from case one with no child and from case 2 with a child).
So case one could be avoided by using savepoint and rollback.
The syntax is to set the save point before any dml and then rollback to that savepoint when you face any error.
Thanku
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?
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm
I realise you asked this almost two years ago, so I am mainly answering for anyone else looking this up. Whether the rollback happens automatically depends on context. For example.
If you transaction called updateParentAndChild without ever catching exceptions, then yes, a failed update to child would rollback the parent. But if instead you caught the exception then it is your resposibility to rollback the transaction. Here is an actual code sample:
Is this case I am simulating an upsert operation. I'm using the rollback, because if the throwErrors flag is true, I want to roll back both the inserts and updates, just like a real upsert call would do. Since this is a method in one of my utilities class, I do not want the results to depend on whether the calling method catches the exception...
Savepoint sp = Database.setSavepoint();
The above line means sp is a variable that will be holding the current state of the database.
Database.rollback( sp );
And the above line means till line where 'Savepoint sp = Database.setSavepoint();' is written the transaction will be rolled back till there.!
Thanks .