In my custom edit page for LineItem__c
records I have overriden the save button with my own implementation which does not only save the record itself but also change to child records.
<apex:page standardController="LineItem__c" extensions="LineItemEditCtrlExt">
<head><title>{!DeliverableLineItem__c.Name}</title></head>
....
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!doSave}" />
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:repeat value="{!$ObjectType.DeliverableLineItem__c.fieldsets.EditBasic}" var="field">
<apex:inputField value="{!lineItem[field]}" />
</apex:repeat>
</apex:pageBlockSection>
To handle errors in an ACID-way I rollback to a Savepoint
in case an exception is thrown.
public with sharing class LineItemEditCtrlExt {
public LineItem__c lineItem { get; set; }
public ApexPages.StandardController stdCtrl;
public LineItemEditCtrlExt(ApexPages.StandardController stdCtrl) {
this.stdCtrl = stdCtrl;
lineItem = (LineItem__c) stdCtrl.getRecord();
}
public PageReference doSave() {
Savepoint toBeforeState = Database.setSavepoint();
try {
upsert lineItem;
saveChildrenX();
saveChildrenY();
return stdCtrl.view();
}
catch(Exception ex) {
ApexPages.addMessages(ex);
// No error when I comment out next line!!
Database.rollback(toBeforeState);
return null;
}
}
...
}
But this always leads to strange
Data not available
errors when the user tries to save again after he was presented with a error message meaning a user clicks Save, sees an validation error on the page and then immediately clicks Save again.
This does not happen at all when I remove the Database.rollback() call. Why is that so? And how should I rewrite my custom save to work?