0

I've got a simple VisualForce page with a form that has one custom picklist. My controller successfully populates the custom picklist and the page renders fine. But when I click on the CommandButton after selecting something int he picklist, the selected value in the picklist doesn't get set and the action method of the CommandButton thinks it's null.

I'm at a loss. I'm sure it's something simple, but I've checked and rechecked it. Here is the code.

VisualForce page:

<apex:page controller="RequestCoverage" showHeader="false" sidebar="false" showChat="false">
<apex:form >
    <script type="text/javascript">
        var isClicked = false;
            function checkDoubleSubmit(obj){
                if (isClicked) {
                    return false;
                }else {
                    isClicked = true;
                    obj.className = 'btnDisabled';//only shows the button as disabled.
                    return true;
                }
            }
    </script>
    <apex:pageBlock >
    <apex:pagemessages ></apex:pagemessages>
        <apex:pageBlockSection columns="1" title="Coverage Request" collapsible="false">
        <apex:pageblockSectionItem >
            <apex:outputLabel >Show</apex:outputLabel>
            <apex:selectList value="{!selectedOption}" size="1">
            <apex:selectOptions value="{!shws}"></apex:selectOptions>
            </apex:selectList>
        </apex:pageblockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Submit Request" action="{!SaveIt}" onclick="return checkDoubleSubmit(this)" />
            <apex:commandButton value="Cancel" action="{!CancelIt}" />
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex controller:

public class RequestCoverage {

    public Coverage_Request__c requestForCoverage { get; set; }
    public String selectedOption { get; set; }
    list<Show__c> activeShows;
    Host__c h;

    class ActiveShowDetail {
        Id showId { get; set; }
        String Show_Display_Name { get; set; }
        String Show_Display_Date { get; set; }
        Date showDate;
    }

    list<ActiveShowDetail> showOptions;

    public RequestCoverage() {
        // A whole bunch of code that I've left out of this post because it's not relevant.
    }

    public pagereference UserLoggedIn(){
        // Kick the user out if a login token wasn't provided in the URL or we couldn't find the host
        if (ApexPages.CurrentPage().getParameters().get('a') == null || h == null) {
            pagereference page = new pagereference('/Hostlogin');
            return page;
        }
        return null;
    }

    public list<selectOption> getShws(){
        list<SelectOption> options = new list<SelectOption>();

         // showOptions is populated in the constructor and this code works fine
         // because the page is successfully rendered
        if (showOptions != null) {
            for (ActiveShowDetail asd : showOptions) {
                options.add(new SelectOption(asd.showId + '/' + asd.Show_Display_Date,asd.Show_Display_Date + ' @ ' + asd.Show_Display_name));
            }
        }

        return options;
    }

    public pagereference SaveIt() {
         // This is the debug statement that is showing that selectedOption is null 
         // even though I selected a value in the picklist
        System.debug('**** Inside of SaveIt()! selectedOption is ' + selectedOption);

        return null;
    }

    public void CancelIt(){
        h.Logged_In_Code__c = null;
        update h;
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'You have been logged out.  Thank you and have a good day.');
        ApexPages.addMessage(myMsg); 
    }
}
1
  • When you remove the onclick from the command button does the page behave the way you expect? – Mark Pond Oct 28 '14 at 20:05
1

I think the problem is your onclick on you commandButton. Salesforce add it's own onclick logic after yours, but yours is always returning. If you change it to: onclick="if(!checkDoubleSubmit(this)) { return false; }" it should work. In that the Salesforce JS will still run if your function returns true. Basically, keep in mind never to return true from the onclick of a commandbutton (or actionlink, or whatever). Maybe it'll make a bit more sense if you inspect the generated javascript of the button with Firebug or something like that.

2
  • Why should one never return true from the onclick of a commandbutton? – eyedar Oct 27 '14 at 16:04
  • 1
    Like I said in my answer; Salesforce adds it's own onclick logic to the custom onclick. So if you return true from your onclick, Salesforce's logic will not be executed. Returning true from an onclick is, almost always, logically the same as not returning anything at all. – rael_kid Oct 27 '14 at 18:20
0

You may want to try adding a rerender attribute to your commandButton, using the pageblock or an added outputPanel container as the target so that you avoid a full page refresh. I ran into an issue very similar to this a while back, and found this blog post from Bob Buzzard that led me to fixing my problem: http://bobbuzzard.blogspot.com/2011/02/visualforce-re-rendering-woes.html

I don't fully understand the mechanics of it, but I believe the rerender submits an Ajax request to check if there are any changes to values on the Visualforce page and updates the corresponding controller variable, if there is one, before actually physically refreshing the page or targeted element. I could be totally making that up as well.

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.