You need to sign in to do that
Don't have an account?
Prachi 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:
Any advise will be greatly appreciated.
Thank you!
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!
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
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.
Thank you!