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
Hariharan M 18Hariharan M 18 

display sum of amount of opportunities in a stage (stage name as parameter-> you can take any one stage name) can anyone help on this?

display sum of amount of opportunities in a stage (stage name as parameter-> you can take any one stage name)
can anyone help on this?
Best Answer chosen by Hariharan M 18
CharuDuttCharuDutt
Hii HariHaran
Try below Code
Public class OppurtunityPracticeClass{
    public static void practiceFunction(){
        decimal sumOfAmount=0;
        string Stage;
        string diffStageName ='Closed Won';
        List<Opportunity> opportunityList = [SELECT Amount,StageName FROM Opportunity WHERE StageName =: diffStageName];
        for (Opportunity opp:opportunityList){
            if(opp.StageName == diffStageName){
                sumOfAmount += opp.Amount;
                Stage = opp.StageName;
            }  
        }
        system.debug(Stage);
        system.debug(sumOfAmount);
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!

All Answers

PriyaPriya (Salesforce Developers) 

Hey hariharan,

Can you kindly elaborate the requirement. Where exactly you want to update the amout amout ? On Account Object?

Thanks,

Priya Ranjan

Hariharan M 18Hariharan M 18
Actually here i need output as below
stage name = closed won
Amount = total amount of opportunities in the stage ' closed won'

as highlighted in the below image if am entering stage name as closed won the the total of amounts highlighted in blue should be calculated,
image

i tried as below

public class OppurtunityPracticeClass {
    
public static void practiceFunction()
    {
    decimal diffStageName ='Closed Won';
        List<AggregateResult> opportunityList = [
                                        SELECT
                                        COUNT(ID) CountofId,StageName, Amount        
                                        FROM 
                                            Opportunity
                                        WHERE
                                            StageName =: diffStageName
                                        GROUP BY StageName
                                        ];
        for (AggregateResult opp:opportunityList)
        {
            system.debug(opp.get('StageName'));
            system.debug(opp.get('CountofID'));
            system.debug(opp.get('Amount'));
        }
    }
}

but am not clear how to calculate the total amount
Hariharan M 18Hariharan M 18
Hope you got the clarity of what I need.
CharuDuttCharuDutt
Hii HariHaran
Try Below Trigger
trigger NumChild3 on Opportunity (After insert,After Update){
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isAfter){
         if(trigger.isInsert){
        for(Opportunity con : Trigger.new){
            if(Con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}else if(trigger.isupdate ){
        for(Opportunity con : Trigger.new){
            if(Con.AccountId != null && (Con.AccountId != trigger.OldMap.get(Con.Id).AccountId)||Con.Amount != trigger.OldMap.get(Con.Id).Amount)){
            setAccIds.add(con.AccountId);
				
            	}
			}
		}
    } 
  list<Account> updateAccounts = new List<Account>();
    for(Account acc :[Select id,SumCLosedAmount__c,(Select id,name,AccountId,Amount,StageName from Opportunities) from Account where Id in : setAccIds]){

 		decimal sumAmount;
      
        for(Opportunity Con :acc.Opportunities){
			if(Con.StageName == 'Closed Won' && acc.Id == Con.AccountId){
			sumAmount+=Con.Amount;
            
			}  
        }
		acc.SumCLosedAmount__c = sumAmount;
       updateAccounts.Add(acc);
    }
   if(updateAccounts.size()>0){update updateAccounts ;}
   
}
Please Mark It As Best Asnwer If It Helps
Thank You!
Hariharan M 18Hariharan M 18
Hi CharuDutt,

I need the code similar to sample i have pasted above,
i need it in SOQL query inside a class.. Not in trigger,,

now i have rewrote the code its working fine but am not able to group it by stage name,
try below code:
Public class OppurtunityPracticeClass{

public static void practiceFunction()
    {
        decimal sumOfAmount=0;
    string diffStageName ='Closed Won';
        List<Opportunity> opportunityList = [
                                        SELECT
                                        Amount,StageName     
                                        FROM 
                                            Opportunity
                                        WHERE
                                            StageName =: diffStageName
                                        //GROUP BY StageName
                                        ];
        for (Opportunity opp:opportunityList)
        {
            
            sumOfAmount += opp.Amount;
            system.debug(opp.get('StageName'));
            //system.debug(opp.Amount);
            
           
        }
        system.debug(sumOfAmount);
    }
}
User-added image

in the output, i need the stage name - closed won to be displayed only once,
but if try to group by in soql its throws error in code.

let me know if you could fix it
CharuDuttCharuDutt
Hii HariHaran
Try below Code
Public class OppurtunityPracticeClass{
    public static void practiceFunction(){
        decimal sumOfAmount=0;
        string Stage;
        string diffStageName ='Closed Won';
        List<Opportunity> opportunityList = [SELECT Amount,StageName FROM Opportunity WHERE StageName =: diffStageName];
        for (Opportunity opp:opportunityList){
            if(opp.StageName == diffStageName){
                sumOfAmount += opp.Amount;
                Stage = opp.StageName;
            }  
        }
        system.debug(Stage);
        system.debug(sumOfAmount);
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!
This was selected as the best answer