1

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');
5

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
| improve this answer | |
  • Thank you. The practice using the same word as an alias in the documentation I find rather confusing. I lost the alias when editing from a sum function and didn't understand the syntax well enough to fix it. – jaw999 Jan 3 '17 at 21:10
1

Your code syntax would be like:-

AggregateResult[] RemainingRevMonths = [SELECT COUNT(id) count 
                                        FROM Deal_revenue__c 
                                        WHERE date__c >: Thisday 
                                        AND Deal__c =: deal.id 
                                        AND Amount__c != NULL];

Decimal RemainingMonthsDec = (Decimal) RemainingRevMonths[0].get('count');

You should define the alias count so you can reference in code to access the data. It can be any other name.

| improve this answer | |
  • 2
    N.B. count is an alias, not a variable – cropredy Jan 3 '17 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.