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
SambitNayakSambitNayak 

SOQL to find all the opportunities related to a Contact

I have a contact and I want to find all the Opportunities related to this contact's parent Account. Is there any single SOQL query to achieve this?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sambit,

Try with below query in your developer console.
[select id,Name,(select id,FirstName,LastName FROM Contacts ),(select id,name from opportunities) From Account where id='Replace account id'];

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
mukesh guptamukesh gupta
Hi Sambit,


FIrst fetch AccountId from Contact that's you have after that use below query :
List<Account> accList = [select id,Name,(select id,name from opportunities) From Account where id='AccountId'];

for(Opportunity opp : accList.opportunities){
  system.debug('opp==>> '+opp);
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
SambitNayakSambitNayak
Hi Mukesh,
Your answer is correct but I was looking for a single SOQL query. Thanks for looking into this though.
mukesh guptamukesh gupta
Hi Sambit,

If you want to fetch All opportunities from Account then you need a sub query that's i have shred with you.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh