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
Bharatsingh Rajpali 6Bharatsingh Rajpali 6 

I want to display the live count of selected checkbox ?

User-added image
Khan AnasKhan Anas (Salesforce Developers) 
Hi Bharat,

Greetings to you!

Try this:
public integer countapprovedrecords=0;

for (myWrapper record : IncList){

 if(record.selected){

  countapprovedrecords++;
        }
}

if (!Apexpages.hasMessages()){
        string countstring = string.valueof(countapprovedrecords);
        system.debug('integer converted to string' +countstring);
        ApexPages.addMessage(new ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Confirm, countstring+  'records selected!'));
                           
}

Also, please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/116367/how-to-count-the-number-of-records-selected-through-a-checkbox

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Bharatsingh Rajpali 6Bharatsingh Rajpali 6
Thanks for the reply....
I actually wanted to display live count 
eg. if a checkbox is checked it should display on the visual force page count = 1 and when deselected then it should display zero....
Deepali KulshresthaDeepali Kulshrestha
Hi Bharat,
Please do try this code below and you will surely be able to display live count of selected accounts on your vf page.

Vf page:
<apex:page controller="CountLiveController">
    <script>
        function test()
      {
          console.log('>>yes');
          methodOneInJavascript();
      }
        
        </script>
    <apex:form id="frm" >
        
        <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="frm"/>

           <apex:pageBlock title="Accounts">

            <apex:pageBlockTable value="{!wrapList}" var="a">
                <apex:column headerValue="Select Account">
                    <apex:inputCheckbox value="{!a.selected}" onchange="test();"/> 
                    
                </apex:column>
                <apex:column headervalue="Account Name" value="{!a.acc.name}"/>
            </apex:pageBlockTable>
             <apex:pageBlockSection>
          Live Count::: {!Count}
            </apex:pageBlockSection>
        </apex:pageBlock> 
    </apex:form>
</apex:page>

Controller class:
public class CountLiveController {
    public List<Account> acList{get;set;}
    public List<AccountWrapper> wrapList{get;set;} 
    public List<AccountWrapper> selectedWrap{get;set;}
    public Integer Count{get;set;}
    public CountLiveController()
    {
        acList=new List<Account>();
        selectedWrap = new List<AccountWrapper>();
        acList=[Select name from Account limit 10];// Show number of accounts as per requirement
        wrapList=new List<AccountWrapper>();
        for(Account a:acList)
        {
           AccountWrapper aw=new AccountWrapper(a);
           wrapList.add(aw); 
        }
        System.debug('>>wrapList'+wrapList);
    }

    public class AccountWrapper{
        public Boolean selected{get;set;}
        public Account acc{get;set;}
      public AccountWrapper(Account a)
      {
          acc=a;
          selected=false;
      }
    }
     
   public void methodOne()
   {
       selectedWrap.clear();
       for(AccountWrapper wrap:wrapList)
       {
          if(wrap.selected==true)
          {
             selectedWrap.add(wrap);
          }
          Count=selectedWrap.size(); 
         system.debug('::::'+selectedWrap);
       }  
   }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Adam Balme 13Adam Balme 13
@Deepali Kulshrestha
How would you modify this so instead of a count you had a sum / aggregation of a currency field on the account record?
e.g. live amount instead of live count.

Many thanks