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
Michael Hedrick 2Michael Hedrick 2 

Validate Visualforce Fields prior to Save

Hello,
I have a Visualforce page that performs the following:
  • saves record
  • shows success message 
  • closes VF page
  • refreshes salesforce page
Here is a snippet of the button.
<apex:commandButton action="{!save}" value="Save" onclick="setTimeout(Closewindow, 5000)" id="saveButton" reRender="Panel" />        
            &nbsp;&nbsp;&nbsp;&nbsp;
            <apex:commandButton value="Cancel" onclick="window.close();" immediate="true"/>
            </div>
            <apex:outputPanel id="Panel" style="font-size:16px;color:red">
                <apex:messages />
            </apex:outputPanel>

The issue is that I have required fields and a message alert the user that the field is required but  the issue is that the 
onclick="setTimeout(Closewindow, 5000)"
still runs. So the VF pages closes without saving the record.
Can the values be checked prior allowing the user select the 'Save' button?

M




 
Best Answer chosen by Michael Hedrick 2
Ashish DevAshish Dev
You need to check your fields for validation through javascript error in Closewindow function. If validation passes call window.close if not show the error.

All Answers

Ashish DevAshish Dev
You need to check length of characters of all required fields on the VF page in the javascript function Closewindow.
Michael Hedrick 2Michael Hedrick 2
Ashish,
Thank you for the reply. I understand that I can put checks in the Controller like this:
if(merch.Merchandise__c== '' || merch.Merchandise__c == null) ApexPages.addmessage(new ApexPages.message (ApexPages.severity.FATAL,'Please select a Merchandise'));

But will it keep the VF page from closing since the syntax below is still in the commandbutton?
onclick="setTimeout(Closewindow, 5000)"
I have a function in the VF page that closes the window and refreshes the details page.  Can the timer go in there?
<script>
   function Closewindow(){
      window.close();
      window.opener.location.href="/{!$CurrentPage.parameters.someId}";
    window.top.refresh();
   }
</script>

 
Ashish DevAshish Dev
You need to check your fields for validation through javascript error in Closewindow function. If validation passes call window.close if not show the error.
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Ashish,
Thanks for your help.  Your suggestion does work but I decided to use the following approach instead.
  • Made all the neccessary fields required on teh VF Page
  • Removed the JS call from the Command Button.
  • Call the JS function that is in the Visualforce Page from the Apex Class.  
Here is the complete code for anyone that might be trying to do the same thing.

Apex
public with sharing class MerchandisingController {

    public Merchandising__c merch{get;set;}
    public string message {get;set;} 
    public string calljavascript{get;set;}
    
    public MerchandisingController (ApexPages.StandardController controller) 
    {
        merch = new Merchandising__c();
        merch.Account__c= ApexPages.CurrentPage().getparameters().get('someId');
    }
        public void save()    
    {
        insert merch; 
         message = 'Record Created Successfully.Please add pictures of the display to the Account. Thank you!'; 
         ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,message )); // Show Success message    in VF page panel
         calljavascript='<script> setTimeout(Closewindow, 5000); </script>'; //call JS function in VF page
    }
          
}
 
VF Page
<apex:page standardController="Merchandising__c"  extensions="MerchandisingController"  standardstylesheets="true" showheader="false"  >

<script>
   function Closewindow() 
   {
     window.close();   <!-- closes VF page--->
     window.opener.location.href="/{!$CurrentPage.parameters.someId}";   <!-- Gets Account page with paramter that was originally used in Account Button --->
     window.top.refresh();   <!-- refreshes Account page--->
   } 
</script>

    <div style="background-color:green;height:500px;width:800px;">
       <apex:form id="Merchandising">
        <apex:sectionHeader title="Choose Merchandizing"/>
        <apex:pageBlock title="Demo Page">
              <apex:pageBlockSection columns="2">             
              <apex:inputField value="{!merch.Merchandise__c}" required="true"/>
              <apex:inputField value="{!merch.Date_Displayed__c}"/>
              <apex:inputField value="{!merch.Merchandise_Category__c}" required="true"/>
              <apex:outputfield value="{!merch.Date_Delivered__c}"  html-disabled="true" />
              <apex:inputField value="{!merch.Merchandise_SubCategory__c}" required="true"/>                
              <apex:inputField value="{!merch.Date_Removed_Replaced__c}"/>
            </apex:pageBlockSection>
            
            <Br></Br><Br></Br><Br></Br>            
            <div align="center" draggable="false" >
            <apex:commandButton action="{!save}" value="Save" id="saveButton" reRender="Panel" />        
            &nbsp;&nbsp;&nbsp;&nbsp;
            <apex:commandButton value="Cancel" onclick="window.close();" immediate="true"/>
            </div>
            
            <apex:outputPanel id="Panel" style="font-size:16px;color:red">
                <apex:messages />
                     <apex:outputText value="{!calljavascript}" escape="false"> </apex:outputText>
           </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
           
           &nbsp;&nbsp;&nbsp;&nbsp;
        
    </div>      
 </apex:page>


 
Ashish DevAshish Dev
Great!!