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
Roshni RahulRoshni Rahul 

To get the total sum of amount field in account object

Hi Team,

Need help.
My requirement is to get the total sum of amount field in account object from opportunity. what i have done is i have created a rollup summary in account object with child as opportunity. So i am getting the total amount of all opportunities associated with an account. Now i have to get total amount for all account. ie sum of the roll up summary. Is that possible..
Best Answer chosen by Roshni Rahul
EldonEldon
Hi Rosh

Please use the below trigger for your req 
 
trigger trialforaccount on Account (after insert, after update) {
    if(checkRecursive.runOnce())
    {
        integer  grandTotal=0;
        Integer Tot;
        list<Account> acc = new list<Account>();
        list<account> newlist = [select Total_amount__c from account];
        
        for(account a : newlist){
            system.debug('totalamnt'+a.Total_amount__c);
            if(a.Total_amount__c!=null)
                grandtotal = grandtotal + integer.valueof(a.Total_amount__c);
            
        }
        
        for(account a : newlist ){
            a.Total_Am__c = grandtotal;
            acc.add(a);
        }
        update acc;
        
    }
    
}


Use this class for avoiding recursive calling of above trigger,
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Please note this calculates only the opportunities having an account. Other opportunities  will be discarded

Regards

All Answers

Roshni RahulRoshni Rahul
I have used the following query,
                    [SELECT Name,CloseDate,amount,sum(account.Total_amount__c) FROM Opportunity]
but its throwing error. Can any one please help me.
pkpnairpkpnair
Your requirement is not completely clear. Do you want to have a total-amount field on the account to show total opportunity values of that account? This can be done with a roll up. 
Then do you also want to have this value on the opportunity to show the total-amount of the account it belongs to? This can be done with a formula  field on the opportunity to populate the total-amount field of its account.
I hope this helps
Roshni RahulRoshni Rahul
Hi,
I have already done what you have mentioned. My requirement is to get the sum of total-amount opportunity value of all accounts. For that I have used the query that i have posted above. But its throwing error while using the SUM. 
Deepak Pandey 13Deepak Pandey 13
my suggestion try Agreegete function in ape code
...
pkpnairpkpnair
Is it a multicurrency org?
pkpnairpkpnair
double  grandTotal;
AggregateResult[] groupedResults = [SELECT sum(Total_amount__c) sum FROM Account where account_type = customer];
grandTotal = double.valueOf(groupedResults[0].get('sum'));

Embed the above logic on your account trigger and save it on account on a field. You can use that field in your SOQL.
EldonEldon
Hi Rosh

Please use the below trigger for your req 
 
trigger trialforaccount on Account (after insert, after update) {
    if(checkRecursive.runOnce())
    {
        integer  grandTotal=0;
        Integer Tot;
        list<Account> acc = new list<Account>();
        list<account> newlist = [select Total_amount__c from account];
        
        for(account a : newlist){
            system.debug('totalamnt'+a.Total_amount__c);
            if(a.Total_amount__c!=null)
                grandtotal = grandtotal + integer.valueof(a.Total_amount__c);
            
        }
        
        for(account a : newlist ){
            a.Total_Am__c = grandtotal;
            acc.add(a);
        }
        update acc;
        
    }
    
}


Use this class for avoiding recursive calling of above trigger,
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Please note this calculates only the opportunities having an account. Other opportunities  will be discarded

Regards
This was selected as the best answer
Roshni RahulRoshni Rahul
Hi Guyz,

Thank you for your replies. Eldon's code is working fine for me.