+ Start a Discussion
APN09217013342392059APN09217013342392059 

using savepoint in extension controller

I have a extension controller for my Visualforce Page.
It has 2 Action methods, method1() and method2().
Each method executes DML statements.

if there is failure in method2(), I need to rollback the DMLs made by method1 ( as well as method2).

I am trying to use SavePoint as a class member because savepoint will be set in method1 and will be accessed in method2 for rollback.

creating a savepoint as class member requires it to be declared as "transient".
If it is transient, the class level savepoint varialble is null, so the Database.rollback(sp) fails.

So does it mean it is not possible to have a savepoint as class member in extension controller ?
NehalNehal (Salesforce Developers) 
HI,

Please refer to links below which will give you sample code on how to use savepoint in apex class:

1.http://salesforce.stackexchange.com/questions/9410/rolling-back-dml-operation-in-apex-method
2.https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yVJIAY
3.https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_transaction_control.htm
4.https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm
5.http://salesforce.stackexchange.com/questions/9905/is-manual-database-setsavepoint-and-rollback-needed-on-trigger-exception

Also there are certain limitations of Savepoint which are listed below:

>>Each savepoint you set counts against the governor limit for DML statements.
>>Static variables are not reverted during a rollback. If you try to run the trigger again, the static variables retain the values from the first run.
>>Each rollback counts against the governor limit for DML statements. You will receive a Runtime error if you try to rollback the database additional times.
>>The ID on an sObject inserted after setting a savepoint is not cleared after a rollback.

I hope this answers your query.


" I hope this information has been helpful. If this has helped resolve your issue, please let us know by marking the post as "Best Answer" to help others in the community with similar questions. "