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
SainSain 

How to display Grand total in VF Page

Hi,

I want to display Grand Total Amount of amount field in expensive object. 

Please help me to achieve this.
User-added image

**VF Page:**

<apex:page controller="ExpensesClass" >

<apex:form >
<!-- Need this variables for totals generation -->
<apex:variable value="{!0}" var="total1"/>

<apex:pageBlock >

<apex:pageBlockTable value="{!expensesList}" var="e" rendered="{!isTableExit}" >

<apex:column value="{!e.Date__c}"/>
<!--<apex:column value="{!e.Item__c}"/>-->
<apex:column >
<apex:outputLink target="_blank" value="/{!e.id}">{!e.Item__c}</apex:outputLink>
</apex:column>

<apex:column headerValue="Amount">
<apex:outputField value="{!e.Amount__c}" />
<apex:variable var="total1" value="{!e.Amount__c+total1}" />
<apex:facet name="footer">
               Total: <span class="t1"></span>   
</apex:facet>
</apex:column>
        
</apex:pageBlockTable>
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Grand Total Amount:"/>
<apex:outputText value="{!totalsum1}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<script>
    // Here we will set the generated subtotals to the footer cells
    
    document.getElementsByClassName('t1')[0].innerHTML = '{!total1}'; 
</script>
</apex:form>
</apex:page>

**Class:**
public class ExpensesClass {
public string nameQuery{get;set;}
public List<Expense__c> expensesList{get;set;}
public Boolean isTableExit {get;set;}
public Date fromdate {get; set;}
public Date todate {get; set;}
public list<AggregateResult> totalsum {get;set;}
public integer totalsum1{get;set;}

//public List<Expense__c> expensesList {get; set;}
public ExpensesClass(){
totalsize=[Select count() From Expense__c];
totalsum=new list<AggregateResult>();
//totalsum=[Select sum(Amount__c) From Expense__c];
}

public PageReference executeSearch(){
string str='%'+nameQuery+'%';
if(nameQuery==null || nameQuery==''){
    //errormessage ='Name cannot be null or empty. Please Try Again.';
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter Owner Name'));
}else{
expensesList=[Select id, Item__c, owner.name,Amount__c, Date__c From Expense__c where owner.name LIKE:str Limit:limitsize Offset:offsetsize];
}

return null;
}

public Void gettotalsum1(){
AggregateResult[] groupedresult=[Select sum(Amount__c) From Expense__c];
object totalsum1=groupedresult[0].get('total');
//return totalsum;
}
}

Thanks in advance 
Sain
 
BharathimohanBharathimohan
Sain,

Did you tried Group By Rollup.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_groupby_rollup.htm

Regards,
Bharathimohan Ramamurthy
Salesforce For All (http://salesforceforall.blogspot.com/)
Shashikant SharmaShashikant Sharma
You seems to be declaring totalsum1 twice once as property and again as Object. Try to make following changes

Remvoe -  public integer totalsum1{get;set;}
 
and update gettotalsum1 return type to Integer

 
public Integer gettotalsum1(){
AggregateResult[] groupedresult=[Select sum(Amount__c) From Expense__c];
object totalsum1val = groupedresult[0].get('total');
return totalsum1val != null ? Integer.valueOf( totalsum1val ) : 0;
}

This should fix the issue.

Let me know if you still face the issue.
SainSain
Hi Shashikant,

Thanks for your support, iam getting below error.

System.SObjectException: Invalid field total for AggregateResult 
Class.ExpensesClass.gettotalsum1: line 33, column 1

regards,
Sain