+ Start a Discussion
Sylvain@RibbonFishSylvain@RibbonFish 

Commandlink not working on first click !

Hi everyone,
I got an issue with my controller and my visualforce page:
When I click on the first button save it display my second section and hide the first one.
When I try to click on the Show All link the time it is doing nothing and I got this exception by javascript:
Uncaught TypeError: Cannot read property 'document' of null
And then I click again and the commandlink is working.
So if you have any idea.
See an example of the code below :)
Thanks
RB

<apex:page standardController="Object__c"  extensions="ObjectPage" >
<style type="text/css">

.checkBoxColumn
{
  max-width: 20px;
}

.headerStyle
{
  background-color:#FFFECD;
  font-weight:normal;
}
</style>
<apex:sectionHeader title="Object Edit" subtitle="Object"/>
       <apex:form >
       <apex:outputPanel id="thePanel"> 
                  <apex:pageMessages id="msgs" escape="false" rendered="{!hasError}"/>
          
        </apex:outputPanel>
       <apex:outputPanel id="section1">
       <apex:pageBlock title="Object Edit" mode="Edit" rendered="{!IF(isSave = false,true,false)}" >
     
      
        <apex:pageBlockButtons location="top">
    
                 
            <apex:commandLink action="{!verificationBeforeSaving}" immediate="true" value="Save" styleClass="btn" style="color:black;text-decoration:none" reRender="section2,section1"/>
                 
        </apex:pageBlockButtons>
       
      
        <apex:pageBlockSection title="Object Information" columns="1" >
          
      
            <apex:pageblocksectionitem >

                    <apex:outputlabel id="hiddenElementId" >Object</apex:outputlabel>
                    <apex:outputpanel layout="block" styleClass="requiredInput">
                        <apex:outputpanel layout="block" styleClass="requiredBlock"></apex:outputpanel>
                       <apex:actionRegion >
                       <apex:selectList  size="1" value="{!selectedObject}"  >
              <apex:selectOptions value="{!ObjectOption}" />
              
              <apex:actionSupport event="onchange" action="{!resetValues}"/>
            
            </apex:selectList>
</apex:actionRegion>
                    </apex:outputpanel>
                </apex:pageblocksectionitem>

               
          
        </apex:pageBlockSection>
       </apex:pageBlock>

       </apex:outputPanel>

       <apex:outputPanel id="section2" >
       <apex:pageBlock title="Apply Object to following Object?" rendered="{!IF(isSave = true,true,false)}"  >
     
        <apex:pageBlockButtons location="top">
                  <apex:commandLink action="{!verificationBeforeSaving}" value="Apply to Selected Object" styleClass="btn" style="color:black;text-decoration:none" >
                <apex:param assignTo="{!applyObject}" value="true" name="applyObject"/>
                  </apex:commandLink>
                 
          </apex:pageBlockButtons>
       
        <apex:outputPanel id="tableContainerObject">

  
<div style="max-height:160px;overflow:auto;">

<apex:commandLink id="test" immediate="true" style="font-size:small;text-align:right;" value="  Show all results" action="{!showAll}"  reRender="test,section2,tableObject"/>

  <apex:pageBlockTable value="{!listToDisplay}" var="a" id="tableObject" headerClass="headerStyle">
<apex:facet name="header"><apex:outputText rendered="{!isHeaderVisible}">Results are narrowed down by criteria set by the administrator. </apex:outputText> </apex:facet>
  <apex:column styleClass="checkBoxColumn">
  <!-- This is our selected   Boolean property in our wrapper class -->
  <apex:inputCheckbox value="{!a.isSelected}"></apex:inputCheckbox>
    
</apex:column>


              </apex:pageBlockTable>
            </div>
          </apex:outputPanel>
      
       </apex:pageBlock>
       </apex:outputPanel>
      </apex:form>
     </apex:page>

--------------------------------------------------------------------------------------------------------------------------------------

public with sharing class ObjectPage {
 
  ApexPages.StandardController GstdController;
    public Boolean hasError {get; set;}
    public Boolean saveAndNew {get; set;}
    Object__c object {get; set;}
    public Id objectId {get; set;}
   
    
    public SelectOption[] objectTypeOption {get; set;}
    public String selectedObjectType {get; set;}

    public Boolean isSave {get; set;}

    public List<Object__c> listToDisplay {get; set;}

    public Boolean applyObject {get; set;}

    public Boolean isHeaderVisible {get; set;}

    // The extension constructor initializes the private member
    // variable mysObject by using the getRecord method from the standard
    // controller.
    public ObjectPage(ApexPages.StandardController stdController) {
       
        GstdController = stdController;
        object = (Object__c)GstdController.getRecord();

        //Get the contact id if we get from the contact page
        if(ApexPages.currentPage().getParameters().get('objectId')!=null)
        {
            objectId =  ApexPages.currentPage().getParameters().get('objectId');

        }

        isSave = false;
        isHeaderVisible = true;
        hasError = true;
        saveAndNew = false;
        applyProduct = false;
        object.Status__c = 'Test';

    }

   

    //This method is call to reset automatically the values when we click a blurb type available in the page
    public void resetValues()
    {
       //Retrive the selected blurb type value
       obj = (Object__c)GstdController.getRecord();

       ObjectType__c currentObjectTypeSelected = informationsByAvailableObjectType.get(selectedObjectType);

    }

    public PageReference verificationBeforeSaving()
    {
        hasError = true;
        object = (Object__c)GstdController.getRecord();
       
        if(selectedObjectType != null )
        {
            object.ObjectType__c = selectedObjectType;
        }

        if(!isSave)
        {
            isSave = true;
            loadObjectUpdateSection();
        }
        else
        {
            return finishRedirectToObject();
        }


        return null;
    }

    //Get the corresponding product to extract the project
    public void loadObjectUpdateSection()
    {
       
        List<Object__c> myList = [Select ID From Object__c Where Status__c = 'Test' Limit 10];
        for(Object obj :myList)
        {
              cObject custObject = new cObject();
              custObject.Test__c = obj;
              listToDisplay.add(custObject);
        }
    }

    public PageReference showAll()
    {
        if(isHeaderVisible)
        {
            isHeaderVisible = false;
            List<Object__c> myList = [Select ID From Object__c  Limit 100];
        for(Object obj :myList)
        {
              cObject custObject = new cObject();
              custObject.Test__c = obj;
              listToDisplay.add(custObject);
        }

        return null;
        }
        else
            isHeaderVisible = true;

        return null;

    }

    public void selectAll()
    {
        for(Object obj : listToDisplay)
        {
            obj.isSelected = true;
        }
    }

    public void deselectAll()
    {
        for(Object obj : listToDisplay)
        {
            obj.isSelected = false;
        }
    }


    public PageReference finishRedirectToObject()
    {
        if(applyObject)
        {
           
            //do something
        }

          
        PageReference pr =  GstdController.save();
       
        if(pr == null)
        {
            hasError = true;
            return pr;
        }
        else{
           
            hasError = false;

           
           
                pr = new PageReference('/' + objectId);
                pr.setRedirect(true);
                return pr;
           
           
        }
    }
}
Ashish_SFDCAshish_SFDC
Hi , 


This is AJAX problem, 

See the below links for more information, 

http://salesforce.stackexchange.com/questions/5553/why-is-the-view-command-link-working-only-the-first-time-i-click-it-how-do-i

Also see below for AJAX related threads,

http://stackoverflow.com/questions/13720249/commandlink-with-ajax-works-only-on-second-click

http://stackoverflow.com/questions/17146545/richfaces-a4jcommandbutton-first-click-does-not-work-the-action-it-works-in-t

http://stackoverflow.com/questions/3658304/jsf-ajax-updates-makes-commandlink-fails-at-the-first-click


Regards,
Ashish