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
IanM1963IanM1963 

Page runs query twice

Hi,

 

I have a page with a drop down where users can select an ID basically that is passed as a parameter back to the controller.

 

Now it all works fine and displays records as it should when the user selects a valid entry. However, when the user selects "none"

 

then the query runs and returns no rows and flashes no rows on the screen. Then it runs AGAIN with the PREVIOUS selection and gets some results!

 

How is this possible??

 

Basically it looks like the query is always runnning twice and picking up the last successful value. So even if I change things like to cycle through weeks of records, it does it then reverts back to the one before...

 

 

<p>Working on forward and back buttons to allow cycling through successive weeks...</p>
<br />
<br /><br />
<table id="t1"><tr><td><h2 class="left">Week Commencing:</h2> <apex:inputField value="{!proxyObject.closeDate}" id="theStartDate" styleClass="left"    /> </td>
                   <td><h2 class="left">Site: </h2><apex:selectList id="theSite"  size="1"   styleclass="left">
                        <apex:selectOptions value="{!ClientSites}"></apex:selectOptions>
                        

                    </apex:selectList>
                    </td>
</tr>
<apex:actionStatus startText="Retrieving records..." id="status"   startStyle="color:red;font-size:20px;"  />
<apex:pageMessages id="errors" />
</table>
<apex:pageBlock mode="edit" id="theBlock">
<apex:pageBlockButtons >
<apex:commandButton value="<< Prev" id="prev" rerender="theBlock,errors" />
<apex:commandButton value="Go" id="theGo" onclick="goToSite();" rerender="theBlock,errors"/>
<apex:commandButton onclick="goNext();" value="Next >>" id="next" rerender="theBlock,errors" />

</apex:pageBlockButtons>

<apex:outputPanel rendered="{!IF(siteId == null, false, true)}" >
    <apex:pageBlockTable value="{!calendars}" var="aColleague" id="theTable"  >
        <apex:column styleClass="cyan" >
                        <apex:facet name="header">
                <b>SiteId</b>
            </apex:facet>
           {!aColleague.Colleague__r.Account__c}
        </apex:column>
        <apex:column styleClass="cyan">
            <apex:facet name="header">
                <b>ColleagueId</b>
            </apex:facet>
            {!aColleague.Colleague__r.Id}
        </apex:column>
        <apex:column styleClass="cyan" >
                        <apex:facet name="header">
                <b>Last Name</b>
            </apex:facet>
           {!aColleague.Colleague__r.Name}
        </apex:column>
        <apex:column styleClass="cyan">
            <apex:facet name="header">
                <b>First Name</b>
            </apex:facet>
            {!aColleague.Colleague__r.Worker_First_Name__c}
        </apex:column>
        <apex:column styleClass="cyan">
            <apex:facet name="header">
                <b>Mobile No.</b>
            </apex:facet>
            {!aColleague.Colleague__r.UK_Mobile_Number__c}
            
        </apex:column>
        <apex:column styleClass="grey">
        <apex:facet name="header">
            <b>Status</b>
        </apex:facet>
        
        <apex:inputField styleClass="{!aColleague.Status__c}" value="{!aColleague.Status__c}" onchange="changeClass(this);" />
            
        </apex:column>
        <apex:column rendered="false" >
            <apex:facet name="header">
                <b>Wk. Comm</b>
            </apex:facet>
            <apex:outputText value="{0,date,dd'/'MM'/'yyyy}">
            <apex:param value="{!aColleague.Wk_Com__c}" /> 
            </apex:outputText>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Sun</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Sunday__c}" value="{!aColleague.Sunday__c}" onchange="changeClass(this);" />
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Mon</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Monday__c}" value="{!aColleague.Monday__c}" onchange="changeClass(this);" />
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Tue</b>
            </apex:facet>
                <apex:inputField styleClass="{!aColleague.Tuesday__c}" value="{!aColleague.Tuesday__c}" onchange="changeClass(this);"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Wed</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Wednesday__c}" value="{!aColleague.Wednesday__c}" onchange="changeClass(this);"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Thu</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Thursday__c}" value="{!aColleague.Thursday__c}" onchange="changeClass(this);"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Fri</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Friday__c}" value="{!aColleague.Friday__c}" onchange="changeClass(this);"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <b>Sat</b>
            </apex:facet>
            <apex:inputField styleClass="{!aColleague.Saturday__c}" value="{!aColleague.Saturday__c}" onchange="changeClass(this);"/>
        </apex:column>

    </apex:pageBlockTable>
 </apex:outputPanel>   
            
              <apex:pageBlock title="Debug" id="debug" rendered="false">
      <apex:outputText value="{!debugSoql}" />           
  </apex:pageBlock>
       </apex:pageBlock>
   
   <script type="text/javascript">
    jQuery.noConflict();
    function doSave(sel,etc) {
    myFunc(sel.value,etc.value);
     
}
</script>

<script type="text/javascript">
    
jQuery.noConflict();
    function changeClass(myElement)
        {      
                 jQuery(myElement).removeAttr('class');
                 jQuery(myElement).addClass(myElement.value);
           }
           
        
    
</script>

<script>
function goNext()
        {    
            var j$ = jQuery.noConflict();
               
            
             var wkComm = j$("#theCal\\:theForm\\:theStartDate").val();
             var siteId = j$("#theCal\\:theForm\\:theSite").val();
               nextMonth(siteId, wkComm);
           }
</script>
<script>
function goToSite()
        {    
            var j$ = jQuery.noConflict();
            var wkComm = j$("#theCal\\:theForm\\:theStartDate").val();
             var siteId = j$("#theCal\\:theForm\\:theSite").val();
             
               goSite(siteId,wkComm);
           }
</script>


       <apex:actionFunction name="myFunc" action="{!saveCalendars}" rerender="theBlock" >       
       <apex:param name="site" value="" />
       <apex:param name="wkcom" value="" />
       
       </apex:actionFunction> 
       
              <apex:actionFunction name="nextMonth" action="{!next}" rerender="theBlock" >       
       <apex:param name="siteId" value="" />
       
       
       </apex:actionFunction> 
       <apex:actionFunction name="goSite" action="{!doSearch}" rerender="theBlock" status="status" >    
           <apex:param name="siteId" value="" />
           <apex:param name="wkComm" value="" />
       </apex:actionFunction>       
    </apex:form>

</apex:page>

 

Here is the code...

 

public  class CalendarController 
{

    
public List<Colleague_Calendar__c> calendars {get; private set;}

   public String soql{get; set;}
   public String siteId{get; set;}
   private Date convDate {get; set;}
   
   public CalendarController()
    {
       calendars = null;

    }

   public void getWeeklyCalendars() 
   {  
        
           
           try 
           {
               calendars = Database.query(soql); 
               System.debug('Size of result:' + calendars.size());
               if (calendars.size() == 0)
               {
                   calendars = null;
               }
           }
           catch (Exception e)
           {
               
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please choose site from the pick list!'));
            }
            
    }
    
 
 
 
 
 
 
 /********************************************************************/
 
 public PageReference doSearch() {
       
         
        
        try 
        {       
            
                siteId = ApexPages.CurrentPage().GetParameters().get('siteId'); //'001A000000i5LScIAM';//
                String tempDate = ApexPages.currentPage().getParameters().get('wkComm');
                convDate = XMLStringToDate(tempDate);
                soql =  'select  Colleague__r.Account__c, Colleague__r.Name,Colleague__r.Worker_First_Name__c,Colleague__r.UK_Mobile_Number__c, Status__c, Wk_Com__c, Monday__c, Tuesday__c, Wednesday__c, Thursday__c, Friday__c, Saturday__c, Sunday__c from Colleague_Calendar__c where Colleague__r.Account__c = :siteId and Wk_Com__c = :convDate and Colleague__r.Account__c != null  order by Colleague__r.Name asc';
                System.debug('Site in Search: ' + siteId);
              
          

            getWeeklyCalendars();
        }catch (Exception e)
           {
               
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please choose site and date from the pick list!'));
            }
        
        
        return null;
   
 }
 
 
 
 
 public PageReference next() {
        return null;
    }
 
 
 
 
 
 public List<selectOption> getClientSites() 
    {
        List<selectOption> options = new List<selectOption>();
        // We don't need this at the moment, remember TODO: test for invalid Id! 
         options.add(new selectOption('', '- None -')); 
        
        for (Account sites : [select Id, Name from Account])
        {
            options.add(new selectOption(sites.Id, sites.Name));
        }
        
        return options;
    }
 
 public static Date XMLstringToDate(String s)
        {
          //XML Date String is in the format dd/mm/yyyy
          String[] stringDate = s.split('/');
          Integer d =  Integer.valueOf(stringDate[0]);
          Integer m = Integer.valueOf(stringDate[1]);
          Integer y = Integer.valueOf(stringDate[2]);
          return date.newInstance(y,m,d);
        }
 
 // Workaround for getting a SalesForce style date picker...
    
   Opportunity o = new Opportunity();
   public Opportunity getProxyObject() { return o; }
 
 
 


    public String debugSoql { get; set; }

    public PageReference saveCalendars() {
        return null;
    }
     
   

}

 

 

I really hope someone can help with this. I've been looking at it for 12 hours :-(

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Now first look to your code suggest me you select a option in picklist ClientSites and then click on Command Button "Go",

 

now here is your command button

<apex:commandButton value="Go" id="theGo" onclick="goToSite();" rerender="theBlock,errors"/>

 it calls actionFuction through goToSite JS function, 

 

just change this like

 

<apex:commandButton value="Go" id="theGo" onclick="return goToSite();" rerender="theBlock,errors"/>

 onclick="return goToSite();"  I have added return 

And your JS function like

 

function goToSite()
        {    
            var j$ = jQuery.noConflict();
            var wkComm = j$("#theCal\\:theForm\\:theStartDate").val();
             var siteId = j$("#theCal\\:theForm\\:theSite").val();
             
               goSite(siteId,wkComm);
             return false;
           }

 

I added return false in JS function

 

Just try above suggestion, let me know with the outcome.

All Answers

Shashikant SharmaShashikant Sharma

Now first look to your code suggest me you select a option in picklist ClientSites and then click on Command Button "Go",

 

now here is your command button

<apex:commandButton value="Go" id="theGo" onclick="goToSite();" rerender="theBlock,errors"/>

 it calls actionFuction through goToSite JS function, 

 

just change this like

 

<apex:commandButton value="Go" id="theGo" onclick="return goToSite();" rerender="theBlock,errors"/>

 onclick="return goToSite();"  I have added return 

And your JS function like

 

function goToSite()
        {    
            var j$ = jQuery.noConflict();
            var wkComm = j$("#theCal\\:theForm\\:theStartDate").val();
             var siteId = j$("#theCal\\:theForm\\:theSite").val();
             
               goSite(siteId,wkComm);
             return false;
           }

 

I added return false in JS function

 

Just try above suggestion, let me know with the outcome.

This was selected as the best answer
IanM1963IanM1963

Hi Shashikant,

 

Thank you so much for this. That fixed it and it now works as it should.

 

Thanks again.

 

Regards,

 

Ian:smileyhappy:

Shashikant SharmaShashikant Sharma

Your welcome Ian,

 

I am also happy it worked for you :)