Activity Forums Salesforce® Discussions savepoint in salesforce

  • savepoint in salesforce

    Posted by Prachi on September 14, 2018 at 2:08 pm

    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?

    Parul replied 5 years, 6 months ago 4 Members · 3 Replies
  • 3 Replies
  • Avnish Yadav

    Member
    September 14, 2018 at 5:29 pm

    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.

    Thanks.

  • shariq

    Member
    September 16, 2018 at 1:28 pm

    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

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

    // 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].
    AccountNumber);

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

    Hope this helps.

  • Parul

    Member
    September 16, 2018 at 2:47 pm

    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();
    try{
    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.

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos