function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prachi SPrachi S 

SavePoint and Rollback in Apex when calling multiple Apex class from Visualforce Page

Hi All,
I have a visualforce page that calls an Apex controller - DataLoadFileUploader which in turn calls an additional Apex Batch class-CSVReaderBatch.

Now both these classes have DML operations and I want to ensure if any of these DML operations fail a complete rollback happens and also an error displays on the page. To achieve this I have defined only 1 savepoint in as shown in below snippet:
 
public class DataLoadFileUploader {

    public Blob contentFile{get;set;} 
    public String nameFile{get;set;}  
    public Boolean IsWhatIfFile{get;set;}
    			
    /***This function calls a batch class that reads the input CSV file and creates records***/
    public Pagereference ReadFile()
    {
    	Savepoint spMain = Database.setSavepoint();
        try{
             //some records are inserted here

        	system.debug('Call CSVReader---');
        	CSVReaderBatch BatchObj = new CSVReaderBatch(contentFile,IsWhatIfFile);
        	Database.executeBatch(BatchObj,2000);                                	        
        }
        catch(Exception e){
        	Database.rollback(spMain);        	
        	ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file:' +e.getMessage());
        	ApexPages.addMessage(errormsg);
        }   
        return null;      
    }       
}
Is this the correct way of ensuring a complete rollback ?

Any advise will be greatly appreciated.

Thank you!


 
Best Answer chosen by Prachi S
Prateek Singh SengarPrateek Singh Sengar
Hi Prachi,
Unfortunately salesforce platform doesnt consider the whole batch execution as a single transaction. Also your apex class in synchronus transaction where the batch execution will be in asynchronus mode. Therefore they are not tied into a single transaction.

However, Salesforce by itself have a rollback mechanism for the failed batches of apex batch class. If a transaction fails in batch execution salesforce will rollback the whole batch class. However it will continue processing the next batches. An alternative could be to write a failure handling logic in the finish method of your batch class.

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Prachi,
Unfortunately salesforce platform doesnt consider the whole batch execution as a single transaction. Also your apex class in synchronus transaction where the batch execution will be in asynchronus mode. Therefore they are not tied into a single transaction.

However, Salesforce by itself have a rollback mechanism for the failed batches of apex batch class. If a transaction fails in batch execution salesforce will rollback the whole batch class. However it will continue processing the next batches. An alternative could be to write a failure handling logic in the finish method of your batch class.
This was selected as the best answer
Prachi SPrachi S
Oh it never occured to me that batch was an aysnc transaction. It makes complete sense now.

Thank you!