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
jovajova 

Simple SOQL Query

Hey All, 

 

I'm trying to retrieve a list of distinct Account IDs by the most recent closed--won opportunity. 

 

In theory, the query below would get me somewhat close, but of course I can't structure my query this way....

 

SELECT id, (SELECT id from Opportunity WHERE iswon = true ORDER BY closedate DESC) from Account

 

any kind and smart souls out there?

Force.comForce.com

Hello jova, what is 'iswon' in the inner query ? If it is a custom field , use iswon__c or its API name and frame your query as:

 

 

SELECT id, (SELECT id from Opportunity WHERE iswon__c =:'true' ORDER BY closedate DESC) from Account;
 

 

Try this

 

WesNolte__cWesNolte__c

Hey

 

You can nest your query like that, but you may have to remove the ORDER BY and DESC keywords. To get a distinct list you can push the Id values into a SET e.g.

 

Set<Id> distinctIds = new Set<Id>();

for(Account a: Accounts){

  distinctIds.add(a.id);

}

 

Wes