Aggregate count error – please assist and excuse this basic mistake.
I am trying to count a number from the query and then to use it in some arithmetic.
Tring to get RemainingMonthsDec throws the error:
System.SObjectException: Invalid field count for AggregateResult:
Trigger
Here is code – thanks
AggregateResult[] ActualizedAmount = [
SELECT SUM(Amount__c )sum
FROM Deal_revenue__c
WHERE date__c >: StartDate
AND Deal__c =: deal.id
AND Amount__c != NULL
];
Decimal ActualizedDecimal = (Decimal) ActualizedAmount[0].get('sum');
AggregateResult[] RemainingRevMonths = [
SELECT COUNT(id)
FROM Deal_revenue__c
WHERE date__c >: Thisday
AND Deal__c =: deal.id
AND Amount__c != NULL
];
Decimal RemainingMonthsDec = (Decimal) RemainingRevMonths[0].get('count');
Decimal RemainingAmount = deal.Campaign_Amount__c - ActualizedDecimal;
Decimal ReaminingSplitAmount = (RemainingAmount / RemainingMonthsDec);
Thanks for the solutions. Per my experience, a small change of making the alias something other than the exact same word used in the aggregate function makes tracking and understanding the meaning of the alias easier. Example:
AggregateResult[] RemainingRevMonths = [SELECT COUNT(id) countalias
FROM Deal_revenue__c
WHERE date__c >: Thisday
AND Deal__c =: deal.id
AND Amount__c != NULL];
Decimal RemainingMonthsDec = (Decimal) RemainingRevMonths[0].get('countalias');
Best Answer
You didn't alias the field. Either add a field alias (
count
), or get the first expression (expr0
).Take a look at the
Force.com SOQL and SOSL Reference
for a more in depth explanation: