You need to sign in to do that
Don't have an account?
prasanth puvvada 4
Soql avg,sum,max error in apex program.
I am wrting sql query for getting count,avg,sum,min,max aggregate functions at apex class. but i am getting this error. please help.
Error: Compile Error: Illegal assignment from List<AggregateResult> to Integer at line 12 column 1 public class soqlfunc { public integer count12{set;get;} public integer avg{set;get;} public integer sum{set;get;} public integer max{set;get;} public integer min{set;get;} public soqlfunc() { count12=[select count() from transaction__c ]; avg=[select avg(amount__c) from transaction__c]; sum=[select sum(amount__c) from transaction__c ]; max=[select max(amount__c) from transaction__c ]; min=[select min(amount__c) from transaction__c ]; } }
please use as below:
for(AggregateResult objAgg : [ select sum(amount__c), max(amount__c) from transaction__c ])
{
count12 = (Integer)objAgg.get('expr0');
max = (Integer)objAgg.get('expr1');
}
Please use as I mentioned above....you are getting error because you cant assign AggregateResult into integer variable..
Tyoe casting is needed..
Thanks,
Sandeep
Some more example for you Please check below post for more details:-
http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm
Please let us know if this will help you
and tested in anoymous window. why the debug line not visibe ?
query is:- SELECT sum(price__c) sum FROM Inventory__c
workbench answer:- 5.8090315656724E+15
VF page answer:- 143733.0
waiting for your help.
I have tested the same code on opportunity object in developer console working fine for me
Please let us know if this will help you
Option 1:-
like below image :-
Option 2:- You can execute your class by executing below code in workbanch:-
soqlfunc obj = new soqlfunc();
STEP 1:- Login on Workbanch
STEP 2:- Then go to Apex Execute
STEP 3:- Exceute below code :- or exceute below code :-
Please let us know if above post will help you. Please mark the same as solution so that if any one have same issue this post will help