4

I have a visualforce page which needs to be auto saved.Currently have two issues with my page.

  1. 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".
  2. 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;

  }
}
  • Visualforce page is not dispalying in my post.Can someone please edit it. – sfdc Oct 31 '13 at 15:54
  • you can edit your own questions. Just use the button {} to make code show up unformatted. – Ralph Callaway Oct 31 '13 at 15:58
  • nevermind, something funky is going on, not sure why it's not formatting correctly – Ralph Callaway Oct 31 '13 at 15:59
  • 2
    The whitespace before a start tag should match the whitespace after. Also, be sure that tabs aren't intermixed with spaces in the whitespace before VF (or HTML) markup and JavaScript. I fixed these issues in your post, and it's displaying correctly. – Matt K Oct 31 '13 at 16:10
5

Short Answer

You need to turn off immediate and adjust or disable your validation rules.

Explanation

Both issues you're having are related to the immediate=true parameter on the action function. This parameter forces the action function to run immediately without running (1) any getters/setters, and (2) any front-end validations.

For (1), since the getters and setters aren't run, none of the field changes make it back to your controller. Hence the record you're saving doesn't include any of the new input.

For (2), you're only bypassing front-end validations, specifically fields you've marked as required in the visualforce page. Validation rules are ALWAYS run no matter what, you can't turn them off. You'll need to disable them or adjust them so they don't apply to new records.

  • Can I move the standard validation rules to controller and make it to run on Save Button not on Auto Save? – sfdc Oct 31 '13 at 17:03
  • Definitely, the key is that point and click validation rules will always run regardless of immediate=true. Moving them into the controller will let you run them selectively for save and not autoSave – Ralph Callaway Oct 31 '13 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.