0

Trying to build a string with the the parts of a SOQL query. I cant get the syntax right with the Account.name and Count fields, even tried the alias name and 'co'.

`List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                                    FROM Case
                                                    WHERE CreatedDate = LAST_N_DAYS:7 AND Id in :Trigger.New
                                                    GROUP BY Account.Name, AccountId
                                                    HAVING COUNT(Id) >= 8
                                                   ];`

message = 'Account name: ' + string.valueOf(AggregateResultList[0].name) +
                ' has ' + integer.valueOf(AggregateResultList[0].co)+
                ' cases opened in the last 8 days.';

I am receiving errors:

Variable does not exist: name
Variable does not exist: co

Thank you in advance.

1 Answer 1

4

With AggregateResult, you need to use the .get(String) method to access things.

e.g. AggregateResultList[0].get('name');

The return value of the sobject's .get() method is an Object, which means you'll need to do a typecast to make the result usable. Something like String name = (String)AggregateResultList[0].get('name');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .