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
Jeremy.NottinghJeremy.Nottingh 

Visualforce page loses field value when refreshing

I am displaying a datatable for a wrapper class that includes a "Selected" checkbox and a custom object, Oppdoc__c. There is a datetime input box(bound to custom object Query__c to use the date picker gadget), and I want to display the records that have CreatedDate greater than or equal to the datetime input. This works, enter a datetime and hit the Refresh button, and the list updates.

 

I also have a Select All checkbox, and if clicked, it should check or uncheck all the boxes currently in the table. However, if this is checked, it resets the entire table to the default state, ignoring the query field. It seems as though the apex code is losing the value of the Query object when SelectAll is invoked. Here is my code:

 

Controller:

public class OppDocTricks { private static OppDoc__c doc; private static Shipment__c shp; private static String pagebreakstring = 'page-break-after: always;'; private static Integer maxdocs; private static Query__c tempq; //to use for query info / date picker private static boolean IsAllSelected = false; private static list<xdoc> displaylist {get;set;} public static Query__c getQuery() { if (tempq == null){ system.debug('resetting tempq right now'); tempq = new Query__c(); } if (tempq.After_Date__c == null) tempq.After_Date__c = datetime.now().adddays(-90); return tempq; } public static void setQuery(Query__c q) { tempq = q; } public boolean getIsAllSelected() { return IsAllSelected; } public void setIsAllSelected(Boolean AllSelected){IsAllSelected = AllSelected;} public OppDocTricks() { if (ApexPages.currentPage().getParameters().get('id') != null) doc = [select id, name from OppDoc__c where id = :ApexPages.currentPage().getParameters().get('id')]; } public class xdoc { OppDoc__c d; boolean Selected; public xdoc( OppDoc__c oppdoc) { d=oppdoc;} public OppDoc__c getOppDoc() { return d; } public boolean getSelected() { return Selected; } public void setSelected(boolean sb) { Selected = sb; } } public static list<xdoc> getQueryinvs() { map<String, id> rtmap = new map<String, id>(); util.mapRecordTypes(rtmap, 'OppDoc__c'); tempq = getQuery(); system.debug('tempq '+ tempq.After_Date__c); if (displaylist != null){ for (xdoc x : displaylist){ system.debug('xd ' + x.d.CreatedDate); } } if (displaylist == null){ displaylist = new list<xdoc>(); for (OppDoc__c odoc : [select CreatedDate, Status__c, Shipto_Name__c, ShiptoZip__c, ShiptoState__c, ShiptoLine2__c, ShiptoLIne1__c, ShiptoCountry__c, ShiptoCity__c, Shipping_Address__c, Purchase_Total__c, Purchase_Order__c, Name, DueDate__c, Document_Notes__c, Date__c, Current_Due_Total__c, Current_Due_Subtotal__c, BilltoZip__c, BilltoState__c, BilltoName__c, BilltoLine2__c, BilltoLine1__c, BilltoCountry__c, BilltoCity__c, Accounting_Entry_Total__c, opportunity__r.Hope_Order_Number__c, opportunity__r.Opportunity_Number__c, Opportunity__r.Name, (select Name, Type__c, Total_Amount__c, Purchase_Quantity__c, Purchase_Net_Amount__c, Product_Code__c, Entry_Date__c, Description__c, Currently_Due__c From Line_Items__r) from OppDoc__c where recordtypeId = :rtmap.get('Invoice') and CreatedDate >= :tempq.After_Date__c]) { system.debug('newxdoc created ' + odoc.CreatedDate); xdoc newxdoc = new xdoc(odoc); //newxdoc.Selected = true; displaylist.add(newxdoc); } }else{//(must not be null) for (Integer counter = 1; counter == displaylist.size(); counter++){ system.debug('counter ' + counter + ' qinv ' + displaylist[counter].d.CreatedDate); if (displaylist[counter].d.CreatedDate < tempq.After_Date__c){ displaylist.remove(counter); } } } return displaylist; } public static void RefreshQuery() { displaylist = null; system.debug('tempq ' + tempq); } public void SelectAll() { system.debug('selectall anyone? isallselected is ' +IsAllSelected); for (xdoc d : displaylist) { system.debug('d selected before ' + d.Selected); d.SetSelected(IsAllSelected); system.debug('d selected after ' + d.Selected); } } }

 

Visualforce page:

 

<apex:page controller="OppDocTricks" cache="true" title="OppDoc Query page" > <style> .OppDocTable{ font-size: 12px; text-align: right; padding: 20px; } </style> <apex:form > <apex:pageblock title="Query OppDocs"> <apex:outputpanel id="table"> <apex:datatable styleclass="OppDocTable" value="{!Queryinvs}" var="inv"> <apex:column > <apex:facet name="header" > <apex:inputcheckbox value="{!IsAllSelected}" > <apex:actionSupport rerender="table" event="onclick" action="{!SelectAll}" status="redrawStatus"/> </apex:inputcheckbox> </apex:facet> <apex:inputcheckbox value="{!inv.Selected}"/> </apex:column> <apex:column value="{!inv.OppDoc.CreatedDate}"> <apex:facet name="header"> <apex:actionStatus id="redrawStatus" startText=" (refreshing...)" /> </apex:facet> </apex:column> </apex:datatable> Show Invoices created since: <apex:inputfield value="{!Query.After_Date__c}" /> <apex:commandbutton value="Refresh list" action="{!RefreshQuery}" status="redrawStatus"/> </apex:outputpanel> <br/> </apex:pageblock> </apex:form> </apex:page>

 

 

Thanks for your help,

Jeremy Nottingham

Vishal.aspireVishal.aspire

Did you ever find solution to it?

I am facing similar kind of problem.

NaishadhNaishadh

Try this. 

<apex:page controller="Checkbox_Class" Tabstyle="Account">
<apex:form >
<apex:pageBlock Title="Accounts with CheckBoxes">
<apex:pageBlockSection Title="List of Available Accounts">
<apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column >
<apex:facet name="header"> <apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>
</apex:inputCheckbox></apex:facet>
<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox></apex:column>
<apex:column headervalue="Account Name" value="{!a.acc.Name}" />
<apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />
<apex:column headervalue="Phone" value="{!a.acc.Phone}" />
</apex:dataTable>
</apex:pageBlockSection>

<apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS">
<apex:dataTable value="{!SelectedAccounts}" var="s" columnswidth="50px,50px" cellpadding="4" border="1">
<apex:column headervalue="Account Name" value="{!s.Name}" />
<apex:column headervalue="Account Number" value="{!s.AccountNumber}" />
<apex:column headervalue="Phone" value="{!s.Phone}" />
</apex:dataTable>
</apex:pageBlockSection>

</apex:pageBlock>
</apex:form>
<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}    
</script>
</apex:page>

 

 

public class Checkbox_Class 
{
    List<accountwrapper> accountList = new List<accountwrapper>();
    List<Account> selectedAccounts = new List<Account>();
        
    public List<accountwrapper> getAccounts()
    {
        for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 5])
        accountList.add(new accountwrapper(a));
        return accountList;
    }
    
    public PageReference getSelected()
    {
        selectedAccounts.clear();
        for(accountwrapper accwrapper : accountList)
        if(accwrapper.selected == true)
        selectedAccounts.add(accwrapper.acc);
        return null;
    }
    
    public List<Account> GetSelectedAccounts()
    {
        if(selectedAccounts.size()>0)
        return selectedAccounts;
        else
        return null;
    }    
    
    public class accountwrapper
    {
        public Account acc{get; set;}
        public Boolean selected {get; set;}
        public accountwrapper(Account a)
        {
            acc = a;
            selected = false;
        }
    }
}