0

This question already has an answer here:

I have a custom controller with a variable 'count.' I want this to update on the page every time the account list updates. Right now it's static. It just displays the original number of records in the list. How do I make it dynamically update on the page?

public class AccountSearchController{
  private String soql {get;set;}
  public List<Account> Accounts {get;set;}  
    public integer count{
        get{
            count = Accounts.size();
            return count;
        }
        set;
    }


  public AccountSearchController() {

    soql = 'select Name,BillingState,Rollout_Group__c,Rollout_Status__c,SD_RD__c,Business_Launch_Date__c,Franchise_Email__c,X800_Number__c,Franchise_ID__c from Account where Status__c = \'Active\'';
    runQuery();
    count = Accounts.size();
  }

  public void runQuery() {

    try {
      Accounts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
  }

Visualforce Page:

<apex:pageBlockTable value="{!Count}" var="c">
   <apex:column>{!c}</apex:column>
</apex:pageBlockTable>

marked as duplicate by Raul, Adrian Larson soql Aug 16 '18 at 22:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Since this property is derived, you don't need a setter, and you don't need to perform an assignment in your getter at all. Just do

public Integer count {
    get {
        return Accounts.size();
    }
}

Your Visualforce page should not compile, because count is not a list of objects to iterate over in the <apex:pageBlockTable>.

To answer your underlying question, though, you need to ensure that whenever your Visualforce page initiates the method runQuery(), that a partial page rerender is performed that includes the region where your count is displayed. This can often be achieved by wrapping the area in an <apex:outputPanel> component with an id, and targeting that id with a rerender attribute on the <apex:commandButton> or other element that initiates the server call. You have not shown enough of your Visualforce page to be more specific.

Not the answer you're looking for? Browse other questions tagged or ask your own question.