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
SFDCBEESFDCBEE 

Calculate Total Revenue for parent Account with VF page

Hi All,
How to calculate total revenue with all child records sum and display in parent account VF page.
Suppose I'm having Parent Account 'Top-level' with 2 child Accounts 'A' & 'B'.
Account  -- Revenue Field
A -- 100$
B -- 100$
Top Level --- A+B ie 200$

I have Tried Aggregate result but it is showing a list of accounts with the amount but not the total amount. 

In VF I need to show in this format  Top Level Account -- 200$
Please help me I'm new to development.

How can  I do like all accounts in one List or Map and add them? and assign to variable and display the variable.I'm not getting how to put this in Code.
Biswojeet Ray 11Biswojeet Ray 11

Hi 

 

Please use this code

<apex:page Controller="accountClass">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!accountsToPopulate}" var="item">
            
			<!--For account Name-->
            <apex:column value="{!item.accRec.Name}"/>
			<!--For account Total Amount-->
			<apex:column value="{!item.Top_Level_Account}"/>

        </apex:pageBlockTable>

    </apex:pageBlock>

</apex:page>

------------------------------------------

Public class accountClass {
    public List<Account> accounts {get; set;}
	public List<wrapclass> accountsToPopulate {get; set;}
	
	public accountClass() {
	    accounts = new List<Account>();
		accountsToPopulate = new List<wrapclass>();
		accounts = [SELECT Id, Name, (SELECT Id, Amount__c FROM child_Accounts__r)
		    FROM Account];
			
		for (Account acc : accounts) {
		    Integer total = 0;
			If (acc.child_Accounts__r != null) {
			    for (child_Accounts__c childRec : acc.child_Accounts__r) {
				    total = total + childRec.Amount__c;
				}
			}
			accountsToPopulate.add(new wrapclass(acc, total));
		}
	}
	
	//wrapper class
	public class wrapclass {
	    public Account accRec {get; set;}
		public Integer Top_Level_Account {get; set;}
		
		public wrapclass(Account accountRec, Integer totalVal) {
		    accRec = accountRec;
			Top_Level_Account  = totalVal;
		}
	}
}


Kindly let me know if it helps you and please mark as Best Answer.

Thanks,