+ Start a Discussion
mikezmikez 

How to disable the save button before clicking second time.

There is a visualforce page with extension controller class.If we click the save button twice , 2 records are creating at a time.

can we disable the button once the button is clicked just like standard functionality. I tried with different ways , can able to disable the button but could not able to prevent the double click .

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

<script>

function hidefield()

{

 document.getElementById("fieldIDtobehide").style.dispaly = "none";

 return false;

}

</script>

<apex:commandbutton onclick="return hidefield();"/>

<input type="text" id ="fieldIDtobehide" />

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Rehan DawtRehan Dawt

Hi

 

This is one way you can avoid multiple records creation: 

* You can use DML operation "UPSERT" when your button is clicked. 

 

So in case user clicks multiple time only records will get updated

 

 

mikezmikez

This script not working . when we are creating a record related to standard object and once after req fields are filled and click save , all the buttons gets disabled( so no chance of clicking it for the second time) . Here in this customized app it is not getting disabled(blur) , so if i click it twice 2 records are created with same name. 

                  

Suresh RaghuramSuresh Raghuram

when you save a record are you not moving to the detail page of the record. or your page is not getting refresh for the first click and not  loosing values from the fields.Jst check out when a record is saved why you are again with in the same page to have chance to click second time the same  save button .

 

If you had to stay in the same page even after the save button pressed you can make disable by setting a flag 

 

for example 

boolean flag= true;

before clicking the button rendered="{!flag}" onclick="{! ! flag}"

 

If this answers your question make this as solution.

Gunners_23Gunners_23

Yeah you can do that but instead of using rendered use disabled like below

 

<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandbutton action="{!disableButton}" value="Save" disabled="{!flag}"/>

</apex:pageBlockButtons>
<apex:pageBlockSection >

 

Controller code

 

public Boolean flag { get ; set; }

 

 public PageReference disableButton()
   {
       flag = true;
       return null;
   }

 

In constructor make that flag as true so that it won't be disabled for the first time

 

mikezmikez

can able to create the record , once you click the save button it takes like 1-2 sec(approx) time to save the record , with in that time if we click button the next time one more record is created with same name . Here once after clicking save button , it should disable and record has to be saved.

Suresh RaghuramSuresh Raghuram

ok then after first click hide the save button as i said using rerenderd

 

for that what you have to do is

<apex:PageblockButtons id="xyz">

<apex:command button action="{!save}  rendered="{!flag}" rerendered="xyz"/>

</apex:pageblockButtons>

set the flag= true in ur constructor and flag =false when your are going out of save method.

 

Ihope this will solve your problem.

 

Feel free to post if any doubts on this.

 

mikezmikez

not working ,here we should not even go to the controller, everything should be done in client side

 

<apex:pageBlockButtons >  
                    <apex:actionStatus id="go">
                        <apex:facet name="stop">                  
                            <apex:commandButton value="Save" action="{!Save}" status="go" disabled="false"/>
                        </apex:facet>
                        <apex:facet name="start">
                           <apex:commandButton status="go" value="Saving..." disabled="true" />
                        </apex:facet>
                    </apex:actionStatus>

 

I tried this way not working either

Suresh RaghuramSuresh Raghuram

did you use the javascript method as one of the member said when you cant rely on controller , you do the things  using javascript try it.

 

all d best.

mikezmikez

<apex:pageBlockButtons >  
                    <apex:actionStatus id="go">
                        <apex:facet name="stop">                  
                            <apex:commandButton value="Save" action="{!Save}" status="go" disabled="false" rerender = "go"/>
                        </apex:facet>
                        <apex:facet name="start">
                           <apex:commandButton status="go" value="Saving..." disabled="true" />
                        </apex:facet>
                    </apex:actionStatus>

 

Finally this works 

 

Thanks All

VinceCVinceC
Using the code from Mikez last post works very well. One thing I had to add to get my page error messages to show was add an id to <apex:pageMessages/> which I placed above my <apex:pageBlockButtons> section and then add the id to the rerender attribute of the first commandbutton, so that,

<apex:pageMessages id="errmsg" /> 

<apex:actionStatus id="go">
                        <apex:facet name="stop">                 
                            <apex:commandButton value="Save" action="{!Save}" status="go" disabled="false" rerender = "go,errmsg"/>
                        </apex:facet>
                        <apex:facet name="start">
                           <apex:commandButton status="go" value="Saving..." disabled="true" />
                        </apex:facet>
                    </apex:actionStatus>



Anthony FosterAnthony Foster
VinceC, I tried implementing the solution you proposed. I can save the record from my vf page with a controller extension but the page doesn't redirect to the new record that I create. Any ideas how to address that?
VinceCVinceC
Hi Anthony,
In your custom controller extension, at the end of your save method you can use the pagereference class, such as,
public PageReference save(){                       
                insert c;
                return new PageReference('/' + c.Id); 
    }
There are more ways to use this class deatiled in the api docs here,
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_system_pagereference.htm

The very last code exmaple on that page has what you are looking for, "When a user clicks Save, he or she is redirected to the detail page for the account just created:"

Anthony FosterAnthony Foster
Thanks VinceC. The pagerefernece class you referenced is already included in the extension. In IE, it says that it can't open the page in a frame and Chrome just reloads the page.