+ Start a Discussion
Ridhima Saxena 21Ridhima Saxena 21 

How to write dynamic query for sum (aggregateresult) in apex??

Hii Team,
I have a requirement as i want to make sum picklist as whenever i click on it field(sobject picklist) should come which is company but i wanted that My field should get calculated value.
Example : 'Select Sum(field) from sobject;'
Used in LWC
AnudeepAnudeep (Salesforce Developers) 
Here is an example code
String condQueryO='SELECT SUM(Amount) FROM Opportunity WHERE Probability >0';
        condQueryO=condQueryO+' Group By Type';
    condQueryO=condQueryO+' Group By Account';
List<AggregateResult> amtList=new List<AggregateResult>();
system.debug('@@@ condQueryO '+condQueryO);
     amtList = Database.query(condQueryO);        
}catch(Exception e){

Let me know if it helps