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

Code to dynamically disable button prevents page refresh

I have written a visualforce page that consists of a data table listing a collection of a custom object.  I added a button that creates a new custom object which is then added to the list as the page is refreshed.


An additional  requirement is that the user should only be allowed to push this button once a month,  so,  I added a piece of code to the controller to do just that and it's called like so:


 <apex:commandButton action="{!NewRecruit}" value="Recruit DD" id="btnRecruitDD" disabled="!DisableRecruitment}"/>


When the form loads,  the button is disabled correctly if the most recent object is less than one month old.

Problem: If there is no object of  less than one month old,  the button is enabled,  the user presses the button and a new object is created.  At this point,  no  page refresh takes place,  so,  the button is still enabled and the new object has not been appended to the list.  If I hit refresh,  the new object is listed and the button is disabled.


Does anyone have any idea how I can get the page to refresh in this instance?




It might be better to use a trigger or maybe even a custom validation rule to enforce the rule. Just disabling the button is pretty weak as far as security goes. That way even if by some fluke the button is enabled the action won't be permitted. 



Sure a trigger is useful and I've got one in place,  but,  it's not a very elegant solution is it?  I'd like the page to be more proactive by guiding the user rather than letting them play with the buttons etc. to see what is and is not possible.

That's great :) I didn't know you had a trigger in place. I was just suggesting that it would ensure that if by some fluke people managed to make the button active in the html they still wouldn't be able to perform the action. 


You could put the button inside a panel and rerender it after the action is executed. Also the code you posted is missing the opening { in the disabled attribute.




<apex:outputPanel id="myButtons">
<apex:commandButton action="{!NewRecruit}" value="Recruit DD" id="btnRecruitDD" disabled="{!DisableRecruitment}" rerender="mybuttons"/>



Message Edited by Scott.M on 06-05-2009 07:33 AM
Message Edited by Scott.M on 06-05-2009 07:34 AM
Message Edited by Scott.M on 06-05-2009 07:36 AM
Message Edited by Scott.M on 06-05-2009 07:36 AM