+ Start a Discussion
sahitya adminsahitya admin 

how to query the related records based on master record id

I had saved Account record,lets us assume,i had related record for Contact,Opportunity and case.Based on Account Id i have to get the related records,but i dont want to hard select id from Contact,Opportunity,Case like.00136000011558cAAA this is my id,help me out ,,thanks in advance.
Hi ,
Can u please explain what do you mean by "I dont want to hard select id from Contact,Opportunity,Case".
Usually if you use the below query  it will give you the list of related records for each account records.
select name,id ,(select lastname,id from contacts ),(select name,id from opportunities),(select caseNumber,id from cases) from account

sahitya adminsahitya admin
I dont want to hard code the Object Names LIke contacts ..opportunities..cases..
Amit GhadageAmit Ghadage
You can write inline query.
child relationship name can be found on lookup field

User-added image

refer below query : 
Set<id> accountIdSet = new Set<Id>();
accountIdSet.add(accountId) //  add your accountIds to set

List<Account> accList = [Select (Select Name, Amount From Opportunities), (Select LastName, Name From Contacts), (Select Id, CaseNumber, SuppliedName From Cases) From Account a where Id IN :accountIdSet ];