Select your Language

How to use Savepoint and Rollback in salesforce apex

Below is the example of, account created and contact created if any error occurred while creating the contact record then it rollback entire process even account will not be created.


 account a = new account();

 Savepoint sp = Database.setSavepoint();

a.name='ankit';

insert a;

contact c= new contact();

c.email= 'abc@abc.com';

// c.lastname='max';

c.accountid=a.id;

try{

    insert c;

}

catch(Exception e)

{

 Database.rollback(sp);

}






No comments:

Post a Comment