I have a visualforce page which needs to be auto saved.Currently have two issues with my page.
- I have some validation rule on some of the fields in visualforce page.I have set the validation rule not to fire on auto save by setting immediate attribute to true.If I try to save the page by using the save button and if there is some validation rule error then auto save still runs after validation rule error message on page and then i get error "Update failed. First exception on row 0 with id a1DK000000wpHVAAS; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION The vehicle is not being maintained by the rep".
- Not All fields are getting saved through auto save.I am not sure why it is not doing saving all values through auto save.But if i do save through save button then all values are getting saved. Below is my visualforce page.
Page:
<apex:page standardController="Report__c" extensions="newFRExtension" action="{!initialize}">
<apex:pageMessages />
<apex:form >
<apex:actionFunction name="autosave" action="{!autosave}" rerender="out" status="savestatus" immediate="true"/>
<apex:actionStatus id="savestatus">
<apex:facet name="start"> Auto Saving.... </apex:facet>
</apex:actionStatus>
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!saveFR}" value="Save"/>
<apex:commandButton action="{!cancelFR}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection collapsible="true" title="Information">
<apex:inputField value="{!newFR.Manager_vod__c}"/>
<apex:inputField value="{!newFR.Review_Date__c}"/>
<apex:inputField value="{!newFR.Employee_vod__c}">
<apex:actionSupport event="onchange" action="{!updateManager}"/>
</apex:inputField>
<apex:inputField value="{!newFR.Days_in_Field__c}"/>
<apex:outputField value="{!newFR.Name}"/>
<apex:outputText value=""/>
<apex:inputField value="{!newFR.FR_Status__c}"/>
<apex:outputField value="{!newFR.Rep_Acknowledged__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="true" title="Date">
<apex:inputField value="{!newFR.Send_to_Rep_Date__c}"/>
<apex:inputField value="{!newFR.Rep_Acknowledged_Date__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Strengths" columns="1">
<apex:inputField value="{!newFR.Strengths__c}" style="width: 100%;"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Areas for Development" columns="1">
<apex:inputField value="{!newCR.Development_Areas__c}" style="width: 100%;"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Notes" columns="1">
<apex:inputField value="{!newFR.Notes__c}" style="width: 100%;"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Closing" columns="1">
<apex:inputField value="{!newFR.Closing__c}" style="width: 100%;"/>
</apex:pageBlockSection>
</apex:pageBlock>
<script>
window.setTimeout(recursivecall,20000);
function recursivecall()
{
window.setTimeout(recursivecall,20000);
autosave();
}
</script>
</apex:form>
</apex:page>
Controller:
public class newFRExtension
{
public Report__c newFr;
public newFRExtension(ApexPages.StandardController controller) {}
public Report__c getNewFR()
{
if (newFR == Null)
newFR = new Report__c();
return newFR;
}
public pageReference saveFR()
{
try
{
if (newFR.Id == null)
{
Insert newFR;
}
else
update newFR;
pageReference newActionItem = new pageReference('/apex/FRView?id=' + newFR.id);
newActionItem.setRedirect(true);
return newActionItem;
}
catch (DMLException ex)
{
ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, ex.getdmlMessage(0));
ApexPages.addMessage(msg);
return null;
}
}
public pageReference cancelFR()
{
if (newFR.Id != null)
delete newFR;
pageReference cancelFR = new pageReference('/a1D/o');
return cancelFR;
}
public void updateManager()
{
if (newFr.Employee_vod__c != null)
newFr.Manager_vod__c = [Select u.Name, u.ManagerId, u.Id From User u where u.Id = : newFr.Employee_vod__c].ManagerId;
}
public void autosave()
{
if (newFR.Id == null)
Insert newFR;
else
update newFR;
}
}
{}
to make code show up unformatted.