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
VioletViolet 

Getting a List of Accounts with Closed Won opportunities

Trying to create a Trigger handler for a Trigger on Account to find List of Accounts that have opportunities with stage 'Closed Won', but not sure if I have the SOQL query set up right. Any help would be appreciated. Getting error ' unexpected token: 'SELECT''
public class NoAccDelwithClosedWonOpenOpps {
    public static Boolean checkAcc_OppClosedWon_Open(List<Account> listaccts){
        Boolean cantdelete;
        List<Account> listofaccts=[SELECT Id (SELECT Id FROM Opportunity WHERE StageName='Closed Won') FROM Account=:listaccts];      
        return cantdelete;
        }
    
Best Answer chosen by Violet
William TranWilliam Tran
Violet, this will fix the syntax error, but there's still work you need to do to for your functionality to work properly such as populating your boolean based on the result set.
 
List<Account> listofaccts=[SELECT Id , (SELECT Id FROM Opportunities WHERE StageName='Closed Won') FROM Account where id IN :listaccts];     
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
 

All Answers

William TranWilliam Tran
Violet, this will fix the syntax error, but there's still work you need to do to for your functionality to work properly such as populating your boolean based on the result set.
 
List<Account> listofaccts=[SELECT Id , (SELECT Id FROM Opportunities WHERE StageName='Closed Won') FROM Account where id IN :listaccts];     
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
 
This was selected as the best answer
VioletViolet
Thank you William! It worked! Thank you so much for the help, I spent hours trying to fix it. 
William TranWilliam Tran
Violet,

Glad it worked out.

Please choose best answer.

Thanks