+ 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.
Arshadulla.ShariffArshadulla.Shariff

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.

http://salesforce.stackexchange.com/questions/32067/custom-list-view-to-display-vf-page

Thanks 
arshad

Roshni RahulRoshni Rahul
Hi Barry

Try the below custom visual force code and class

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



Class:
 
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;
        }
        set;
    }

    // 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){
            if(opp.amount!=null)
            grandtotal  =grandtotal + opp.amount;
        }
        return grandtotal;
    }
}



Its working perfectly foe me.

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