• #34163
     Prachi #9

    savepoint in salesforce

    If you have set up more than one savepoint, and then you roll back to a savepoint which is not the last savepoint, what will happen to the later savepoint variables?


    Hello Prachi,

    If you set more than one savepoint, then roll back to a savepoint that is not the last savepoint you generated, the later savepoint variables become invalid. For example, if you generated savepoint SP1 first, savepoint SP2 after that, and then you rolled back to SP1, the variable SP2 would no longer be valid. You will receive a runtime error if you try to use it.


    Salesforce | shariq shariq #1


    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

    Account a = new Account(Name = ‘xxx’); insert a;
    System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

    // Create a savepoint while AccountNumber is null
    Savepoint sp = Database.setSavepoint();

    // Change the account number
    a.AccountNumber = ‘123’;
    update a;
    System.assertEquals(‘123’, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

    // Rollback to the previous null value
    System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

    Hope this helps.

    Salesforce | Parul Parul #2

    database.savepoint is a method which is used to define a point which can be roll back to. If any error occurs during a transaction, that contains many statements, the application will roll back to the most recent savepoint and the entire transaction will not be aborted.

    for example,

    Account acc = new Account();
    acc.name =’ABC’;
    —- ——–
    —– some code ———
    Savepoint sp = Database.setSavepoint();
    insert acc;
    catch( Exception e ){
    Database.rollback( sp );
    —— the code continues —–

    In this exapmle, if any error occurs while inserting the acc then the entire transaction will rollbak to savepoint sp ( as specified in the catch section by Database.rollback method).

    It is advisable to use implicit savepoint is marked before executing an INSERT, UPDATE, or DELETEstatement. If the statement fails, a rollback to the implicit savepoint is done.



Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?