function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andreas WissmeyerAndreas Wissmeyer 

GROUP BY with account type - is there a way to do it without using aggregate objects?

Hello all and thanks in advance for your replies on this. I checked similar question but I didn't find something matching my usecase.

My code is really straightforward (it works without GROUP BY): I want to take a series of fields from account when some conditions are met (the last update on an account should be higher than 150 days).

Below the working code (without GROUP BY):

    public Account getAccount() 
        account = [SELECT Id, Name, Last_Status__c,Additional_Information__c,Ongoing_Problem__c,Last_Update__c FROM Account 
                       WHERE (OwnerId = :UserInfo.getUserID()) AND (Type <> 'Dead') AND (Last_Update__c = LAST_N_DAYS:150) 
                       ORDER BY Last_Update__c DESC LIMIT 1];
        return account;

Adding GROUP BY Last_Update__c system says I should aggregate also Id, if I do it it asks for Name and so on until I aggregate with every possible field in the SELECT. This is already not what I want, but even doing this in the end a get a type incompability.

Is there a simple way to have this grouping having still an account at the end of the process?

And btw, is "Last_Update__c = LAST_N_DAYS:150" correct to get only accounts that have NOT been updated in the last 150 days? Using logic, it should be "Last_Update__c > LAST_N_DAYS:150" but I never get any result if I use this expression, even if many accounts in Salesforce should respect this condition.


Andreas WissmeyerAndreas Wissmeyer
I managed to solve this one bypassing the use of GROUP BY, however I am still very interested in your replies on this one.. A.