With my existing VF page and controller, what do I need to do to be able keep the 'save' method from being called by an accidental click? Can I add validation or disable the save button?

When the Saved button is clicked and all of the required fields are not Null. A message appears notifying the user that the record was saved. VF is closed after 5 seconds. Account page reloads.

VF page

<apex:page standardController="Merchandising__c"  extensions="MerchandisingController"  standardstylesheets="true" showheader="false"  >
   function Closewindow(){

<div style="background-color:green;height:500px;width:800px;">
   <apex:form id="Merchandising">
    <apex:sectionHeader title="Choose Merchandizing"/>
    <apex:pageBlock title="Demo Page">
    Delivery Date = '05/18/2018';
        <apex:pageBlockSection columns="2">             
          <apex:inputField value="{!merch.Merchandise__c}"/>
          <apex:inputField value="{!merch.Date_Displayed__c}"/>
          <apex:inputField value="{!merch.Merchandise_Category__c}"/>
          <apex:outputfield value="{!merch.Date_Delivered__c}"  html-disabled="true" />
          <apex:inputField value="{!merch.Merchandise_SubCategory__c}"/>                
          <apex:inputField value="{!merch.Date_Removed_Replaced__c}"/>

        <div align="center" draggable="false" >
        <apex:commandButton action="{!save}" value="Save" onclick="setTimeout(Closewindow, 5000)" id="saveButton" reRender="Panel" />        
        <apex:commandButton value="Cancel" onclick="window.close();" immediate="true"/>

        <apex:outputPanel id="Panel" style="font-size:16px;color:red">
            <apex:messages />



public with sharing class MerchandisingController {

public Merchandising__c merch{get;set;}
public string message {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 disply to the Account. Thank you!'; 
    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,message )); 
  • I know I can add an if statement like ---- if(merch.Merchandise__c== '' || merch.Merchandise__c == null) ApexPages.addmessage(new ApexPages.message (ApexPages.severity.FATAL,'Please enter Account name')); And remove the SetTimeOut from the Command button but how can I still perform the same actions of wait, close, and refresh.
    – Phuc
    Jun 1, 2018 at 0:29

2 Answers 2


The code above restricts the user from perform an accidental click, while the save is being performed, once the Save button is clicked.

Validations are required to ensure consistency in the input data. In order to restrict a Save, when fields are left empty, you can use the required attribute on <apex:inputField> as below.

<apex:inputField value="{!merch.Merchandise__c}" required="true"/>

However, is it important to understand that this technique will only mandate the field, to have a value only on this page. In case, you have the same field being populated with values through other mechanisms such as via an API integration or through a data upload, and require the same mandate to be applied there, you should mark the field as required on the respective Sobject rather than on the Visualforce page.

  • Jigar, thank you for your help. The required field option is helpful and I added it to the VF page. But I am having issues fully understanding the facet component. So to get this requirement completed I was wondering. Would calling a script with field validation and then calling the Closewindow script work? Or call the Closewindow script from the Controller after showing the message? I have a OnClick attribute in the save so it closes the page even with the required fields being null.
    – Phuc
    Jun 1, 2018 at 20:46

With my existing VF page and controller, what do I need to do to be able keep the 'save' method from being called by an accidental click? Can I add validation or disable the save button?

The code below can be used to display the Save and Cancel buttons as disabled, once the Save method is called and completes processing.

Visualforce Code - FacetSample

<apex:page controller="FacetSampleController">

        <apex:form id="theForm">

            <apex:outputPanel id="pnlSave">

            <apex:actionStatus id="saveStatus" onstart="document.body.style.cursor='wait';return true;" onstop="document.body.style.cursor='default';return true;">
                <apex:facet name="start" >                
                    <apex:commandButton id="btnSaveDisabled" disabled="true" status="saveStatus" value="Saving..."/>
                <apex:facet name="stop">
                    <apex:commandButton id="btnSave" action="{!doSave}" reRender="pnlSave" rendered="{!saveBtn}" status="saveStatus" value="Save"/>

            <apex:commandButton id="btnCancel" value="Cancel" action="{!doCancel}"/>



Apex Controller - FacetSampleController

public class FacetSampleController {

    public static Boolean saveBtn {get; set;}

    static{ saveBtn = true;}

    public FacetSampleController(){}

    //Show Save button
    public PageReference showSaveBtn(){
        saveBtn = !saveBtn;
        return null;

    public void doSave(){

        //Hide clickable Save button

        //Dummy processing to add time lag. Replace with actual processing logic
        for(Integer cnt = 0; cnt < 30; cnt++){
            System.debug(LoggingLevel.DEBUG, cnt);

    public void doCancel(){}

As per the Visualforce Developer Guide

A facet consists of content in an area of a Visualforce component that provides contextual information about the data that is presented in the component.

For example, <apex:dataTable> supports facets for the header, footer, and caption of a table, while <apex:column> only supports facets for the header and footer of the column. The <apex:facet> component allows you to override the default facet on a Visualforce component with your own content. Facets only allow a single child within the start and close tags.

  • Jigar, Thank you for the reply. Do I still need to add validation in the Controller to determine if the fields oin the VF page are completed? Also, the Cancel button is visible and enabled which is good just in case the user needs to exit the page. But the Save button is missing. I am not familiar with the apex:facet component so I am still trying to determine how this is controlling the Command buttons.
    – Phuc
    Jun 1, 2018 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .