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
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';
if(true){
        condQueryO=condQueryO+' Group By Type';
}else{
    condQueryO=condQueryO+' Group By Account';
}
List<AggregateResult> amtList=new List<AggregateResult>();
system.debug('@@@ condQueryO '+condQueryO);
try{
     amtList = Database.query(condQueryO);        
}catch(Exception e){
     system.debug(e);
}

Let me know if it helps