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
karthik karthikkarthik karthik 

SOQl on opportunity, Account & Contact

Hi All,

Using "Opportunity Id" how to fetch Account, Opportunity & Contact fields.

Thanks in advance.
Best Answer chosen by karthik karthik
Rajendra RathoreRajendra Rathore
//Query Opportunity
List<Opportunity>lstOppts = [select id,AccountId From Opportunity WHERE Id=:oppId];
if(lstOppts.size()>0){
       //Query Account with their contact records
       List<Account>lstAccounts = [select id,Name,(select id,FirstName,LastName FROM Contacts ) From Account WHERE Id =:lstOppts[0].id];
}

Thanks,
Rajendra

All Answers

Rajendra RathoreRajendra Rathore
//Query Opportunity
List<Opportunity>lstOppts = [select id,AccountId From Opportunity WHERE Id=:oppId];
if(lstOppts.size()>0){
       //Query Account with their contact records
       List<Account>lstAccounts = [select id,Name,(select id,FirstName,LastName FROM Contacts ) From Account WHERE Id =:lstOppts[0].id];
}

Thanks,
Rajendra
This was selected as the best answer
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi, 

You have can use nested Query on Account to get Particular Account, Opportunity & Contact fields.
 
select id, Name, (Select Id, Name from contacts), (Select Id, Name From Opportunities where Id='Your Opportunity Id') from Account
Hope this helps,

--
Thanks,
Swayam
 
karthik karthikkarthik karthik
Thank you Swayam & Rajendra for your suggestion.