+ Start a Discussion
HARSHIL U PARIKHHARSHIL U PARIKH 

How to take a size for this "res": AggregateResult res : [SELECT Custom_field__C];

Hello Developers,

I am trying to take a size for this variable "res" from this SOQL query---> AggregateResult res : [SELECT Custom_field__C];
I am using Count__C = res.size() it throws an error as
Method does not exist or incorrect signature: [AggregateResult].size()
I know I can use .size() on list but I am not sure about on this "res."
Thank you for the help!
Best Answer chosen by HARSHIL U PARIKH
Alain CabonAlain Cabon
Integer count__c = [select count() from your_object__c where Custom_field__C ...];

Integer count = [select count() from account where name like '%a%'];
system.debug('count : ' + count);

or
 
AggregateResult[] groupedResults= [SELECT accountid, count(id) FROM contact where accountid != null GROUP BY accountid];
system.debug('size : ' + groupedResults.size());
system.debug('account #1 : ' + groupedResults[0].get('accountid'));
system.debug('count #1 : ' + groupedResults[0].get('expr0'));
Alain

All Answers

Alain CabonAlain Cabon
Integer count__c = [select count() from your_object__c where Custom_field__C ...];

Integer count = [select count() from account where name like '%a%'];
system.debug('count : ' + count);

or
 
AggregateResult[] groupedResults= [SELECT accountid, count(id) FROM contact where accountid != null GROUP BY accountid];
system.debug('size : ' + groupedResults.size());
system.debug('account #1 : ' + groupedResults[0].get('accountid'));
system.debug('count #1 : ' + groupedResults[0].get('expr0'));
Alain
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Thank you for the answers..
This is the query I have
for(AggregateResult res : [SELECT Individual_Customer__C ,sum(Transaction_Total__c)can FROM Merchandise__C WHERE 
                                                                       Individual_Customer__C IN :ConIdSet group by Individual_Customer__C ]) 
        {
          ConListToUpdate.add(new Contact (Id=(Id)res.get('Individual_Customer__C'), Total_Money_Spent_for_ACA_Products__c = (Double)res.get('can')
                                                      , Transaction_Count__C =  res.size()  ));
        }
Now on Transaction_Count__C I need to update that AggregateResult's size or number of records in that query.

This is the error I am getting,
Method does not exist or incorrect signature: [AggregateResult].size() 
HARSHIL U PARIKHHARSHIL U PARIKH
Thank you guys for the support and I just found the solution, it was just a matter about syntex.
 
for(AggregateResult res : [SELECT Count(id)Quantity, Individual_Customer__C ,sum(Transaction_Total__c)can FROM Merchandise__C WHERE 
                                                                       Individual_Customer__C IN :ConIdSet group by Individual_Customer__C ]) 
        {
          ConListToUpdate.add(new Contact (Id=(Id)res.get('Individual_Customer__C'), Total_Money_Spent_for_ACA_Products__c = (Double)res.get('can')
                                                      , Transaction_Count__C =  (Integer) res.get('Quantity')  ));
        }
Just need to write Transaction_Count__C = (Integer) res.get('Quantity') instead of Transaction_Count__C =  res.size()

Thanks!