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
Adam Balme 13Adam Balme 13 

Display live sum of of currency field of selected records

I have a VF page that displays a live count of selected rows of a VF page. I'd like to also show a sum of a currency field for the selected records. Let's say the field is Total_Amount__c. 

How could I include this live sum in my page.

here is the code:
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:
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);
       }  
   }
}

 
Best Answer chosen by Adam Balme 13
Soyab HussainSoyab Hussain
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 Integer totalAmount{get;set;}
    public CountLiveController()
    {
        totalAmount = 0;
        acList=new List<Account>();
        selectedWrap = new List<AccountWrapper>();
        acList=[Select name, Total_Amount__c  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()
    {
        totalAmount = 0;
        selectedWrap.clear();
        for(AccountWrapper wrap:wrapList)
        {
            if(wrap.selected==true)
            {
                selectedWrap.add(wrap);
                if(wrap.acc.Total_Amount__c != null){
                    totalAmount += Integer.valueOf(wrap.acc.Total_Amount__c);
                }
            }
            Count=selectedWrap.size(); 
            system.debug('::::'+selectedWrap);
        }  
    }
}
 
<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}
                 <br/>
                 Total Amount:::{!totalAmount}
            </apex:pageBlockSection>
        </apex:pageBlock> 
    </apex:form>
</apex:page>

Hi Adam Balme,

If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards 
Soyab

All Answers

Soyab HussainSoyab Hussain
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 Integer totalAmount{get;set;}
    public CountLiveController()
    {
        totalAmount = 0;
        acList=new List<Account>();
        selectedWrap = new List<AccountWrapper>();
        acList=[Select name, Total_Amount__c  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()
    {
        totalAmount = 0;
        selectedWrap.clear();
        for(AccountWrapper wrap:wrapList)
        {
            if(wrap.selected==true)
            {
                selectedWrap.add(wrap);
                if(wrap.acc.Total_Amount__c != null){
                    totalAmount += Integer.valueOf(wrap.acc.Total_Amount__c);
                }
            }
            Count=selectedWrap.size(); 
            system.debug('::::'+selectedWrap);
        }  
    }
}
 
<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}
                 <br/>
                 Total Amount:::{!totalAmount}
            </apex:pageBlockSection>
        </apex:pageBlock> 
    </apex:form>
</apex:page>

Hi Adam Balme,

If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards 
Soyab
This was selected as the best answer
Adam Balme 13Adam Balme 13
That's great thanks for your help.