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.

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

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.