3

How to query child to parent like contact to parent..and how to query master lookup relations.

select id,lastnam,(select id,name from account where accountid=conatctid)from contact

4 Answers 4

3

This just an example on how the parent-child query works...

Ways of writing Parent - Child Queries for Standard Objects

Select Id, Name, (Select Id, LastName from Contacts where Lastname='Frank') from Account

Select Id, LastName, Account.Name from Contact

Ways of writing Parent - Child Queries for Custom Objects

Select Id, Name, (Select Id, Name from Custom_Object2s__r) from Custom_Object1__c

Select Id, Name, Custom_Object1__r.Name from Custom_Object2__c

Hope this might helps...

4
  • are you trying to query Account in subquery? without from ?
    – Ratan Paul
    Jun 20, 2016 at 5:48
  • This query is not working fine Jun 20, 2016 at 5:50
  • @Hemalatha paruchuri, That's k, I didn't understand your question either... hope you find your solution
    – Subhash
    Jun 20, 2016 at 5:52
  • your first query should be Select Id, LastName, (Select id, LastName from Contacts where LastName='ContactName' ) from Account your where and from place need to be change
    – Ratan Paul
    Jun 20, 2016 at 5:58
8

If you want get the parent object information then you need to use like this.

SOQL Child to Parent

for(Contact objContact: [SELECT Id, LastName, AccountId, Account.Name FROM Contact])
{
  system.debug('====Account Name======'+objContact.Account.Name);
}

if you want any other info about Account then you need to use Account.FieldAPIName... this way you will be able to get the Account info

SOQL Parent to child

for(Account objAccount: [SELECT Id, Name, (SELECT Id, LastName FROM Contacts)  FROM Account])
{
    for(Contact objContact: objAccount.Contacts)
    {
       system.debug('====Contact onfo======'+objContact);
    }
 }
6
  • this is the only way to query child to parent in soql Jun 20, 2016 at 5:49
  • @Hemalathaparuchuri yes this is the only way to query from child to parent.
    – Ratan Paul
    Jun 20, 2016 at 5:50
  • ok ratan ..in parent child to parent query [select id.name,{select id,lastname from contacts) from account]; is there any way to query child to parent query Jun 20, 2016 at 6:02
  • @Hemalathaparuchuri no. you can't query like that. I have added both child to parent and parent to child structure in my answer. this is the only way you could use the relationship query. is there anything you are trying to figure it out that i am not aware ?
    – Ratan Paul
    Jun 20, 2016 at 6:06
  • @Hemalathaparuchuri great.
    – Ratan Paul
    Jun 20, 2016 at 6:43
2

You can traverse a relationship from a child object (contact) to a field on its parent (Account.Name) by using dot notation.

You can traverse multiple levels upwards!

List<Contact> list= [Select ID,LastName,Account.Name, Account.Industry from Contact where Name='abcd'];
   for(Contact c: list){
      System.debug('Contact LastName is:' + c.LastName);
      System.debug('Account Name is:' + c.Account.Name);
   }
4
  • .in parent child to parent query [select id.name,{select id,lastname from contacts) from account]; is there any way to query child to parent query.and one more thing how to query lookup relations Jun 20, 2016 at 6:04
  • @Hemalathaparuchuri This the format only. Jun 20, 2016 at 6:07
  • From Parent to child-> Inner Query is used and from child to parent we have to use Dot notation. like above format. Jun 20, 2016 at 6:08
  • Welcome @Hemalathaparuchuri . I am glad that, your doubt is clear. Happylearning:) Jun 20, 2016 at 6:47
-2

we can understand it by an example. We have Account and Contact Object and we know that the relationship between them is lookup relationship. Note : This relationship has one property called cascadedelete that is why, when you will delete a contact, it's parent will got deleted.

Child To Parent SOQL ; [ SELECT Name,Phone,Email,AccountId,Account.Name,Account.Phone,Account.BillingAddress FROM Contact ]

2
  • Please use code quotes for better readability.
    – HasanOnM
    May 17 at 9:50
  • Welcome to Salesforce Stack Exchange (SFSE)! Your contributions on SFSE is welcome, however, this 6-year-old question was answered sufficiently three times 6 years ago. While it is acceptable in general to add new answers to old questions, your answer does not add much extra - except the mention of cascade delete, which the question does not ask about & which you do not explain very clearly. I am assuming these are some of the reasons that your answer has been downvoted. I suggest that you consider deleting this answer, and instead find some unanswered questions that you can help with.
    – Moonpie
    May 17 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.