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
Jonathan Osgood 3Jonathan Osgood 3 

AggregateResult in Expression

Is this possible (with the correct syntax)?
 
if(AggregateResult results = [SELECT SUM(X2_1_a_Total_of_jobs_Year_2__c) FROM Form__c] > results.size(0)){
    
    System.debug('sum of year 2 greater than 0');
}else{
    system.debug('sum of year 2 NOT greater than 0');
}

I know you can use Count() in an expression like this (according to this jeff douglass post (http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/)) because it returns the number of rows, not an object. But can I cast into an integer or something else?
Best Answer chosen by Jonathan Osgood 3
Amit Chaudhary 8Amit Chaudhary 8
Please let us know if above code will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please change your code like below code.
List<AggregateResult> results = [SELECT SUM(X2_1_a_Total_of_jobs_Year_2__c) mysum FROM Form__c]  ;

if( Double.valueOf(results[0].get('mysum')) > 0)
{
    System.debug('sum of year 2 greater than 0');
}else{
    system.debug('sum of year 2 NOT greater than 0');
}
Please let us know if that will help you.Please check below post for more help.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm
 
Amit Chaudhary 8Amit Chaudhary 8
Please let us know if above code will help you
This was selected as the best answer
Jonathan Osgood 3Jonathan Osgood 3
That worked, thank you Amit!