[SalesForce] Invalid field count for AggregateResult:

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).

List<AggregateResult> aggregates = [SELECT count(Id) count FROM ...];

// OR

Decimal count = (Decimal)myAggregate.get('expr0');

Take a look at the Force.com SOQL and SOSL Reference for a more in depth explanation:

Using Aliases with GROUP BY

You can use an alias for any field or aggregated field in a SELECT statement in a SOQL query. Use a field alias to identify the field when you’re processing the query results in your code.

Specify the alias directly after the associated field. For example, the following query contains two aliases: n for the Name field, and max for the MAX(Amount) aggregated field.

SELECT Name n, MAX(Amount) max
FROM Opportunity
GROUP BY Name

Any aggregated field in a SELECT list that does not have an alias automatically gets an implied alias with a format expri, where i denotes the order of the aggregated fields with no explicit aliases. The value of i starts at 0 and increments for every aggregated field with no explicit alias.

In the following example, MAX(Amount) has an implied alias of expr0, and MIN(Amount) has an implied alias of expr1.

SELECT Name, MAX(Amount), MIN(Amount) FROM Opportunity GROUP BY Name

In the next query, MIN(Amount) has an explicit alias of min. MAX(Amount) has an implied alias of expr0, and SUM(Amount) has an implied alias of expr1.

SELECT Name, MAX(Amount), MIN(Amount) min, SUM(Amount)
FROM Opportunity
GROUP BY Name
Related Topic