+ Start a Discussion
Barry Wang 11Barry Wang 11 

Get sum of amount under opportunity

Hello everyone, I just get started with Salesforce development. Is there a possibility that the total amount can show up on the page I post. I want to get the sum of the Amount and display the number on this page. 
User-added image
Ritu RajpalRitu Rajpal
Use formula field or Rollup summary of you wanted to display it on parent object.

Hello Barry,
For achieving that you need, to build customized visual force page and create Visual Force Tab for the page.

For more details refer the following question posted.



Roshni RahulRoshni Rahul
Hi Barry

Try the below custom visual force code and class

<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.Amount}"/>
        <apex:pageBlockSection >
            <apex:pageblocksectionitem >
                <apex:outputLabel value="Grand Total" style="font-weight: bold;"></apex:outputLabel>
            <apex:pageblocksectionitem >
                <apex:outputLabel style="margin-left: 285px !important;font-weight: bold; " value="${!total}"></apex:outputLabel>

public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name,CloseDate,amount,tot__c,Account.Total_Am__c FROM Opportunity ]));
            return setCon;

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    public double getTotal(){
        list<opportunity> totalopp = [select amount,tot__c,Account.Total_Am__c FROM Opportunity];
        double grandtotal = 0;
        for(opportunity opp : totalopp){
            grandtotal  =grandtotal + opp.amount;
        return grandtotal;

Its working perfectly foe me.

Roshni RahulRoshni Rahul
Your result will be like this :
User-added image
Hi Barry if you your problem got solved then please close the thread by selecting the right answer as best answer.