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

1

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...

  • are you trying to query Account in subquery? without from ? – Ratan Paul Jun 20 '16 at 5:48
  • This query is not working fine – Hemalatha paruchuri Jun 20 '16 at 5:50
  • @Hemalatha paruchuri, That's k, I didn't understand your question either... hope you find your solution – Subhash Jun 20 '16 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 '16 at 5:58
7

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);
    }
 }
  • this is the only way to query child to parent in soql – Hemalatha paruchuri Jun 20 '16 at 5:49
  • @Hemalathaparuchuri yes this is the only way to query from child to parent. – Ratan Paul Jun 20 '16 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 – Hemalatha paruchuri Jun 20 '16 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 '16 at 6:06
  • @Hemalathaparuchuri great. – Ratan Paul Jun 20 '16 at 6:43
1

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);
   }
  • .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 – Hemalatha paruchuri Jun 20 '16 at 6:04
  • @Hemalathaparuchuri This the format only. – Archita patnaik Jun 20 '16 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. – Archita patnaik Jun 20 '16 at 6:08
  • Welcome @Hemalathaparuchuri . I am glad that, your doubt is clear. Happylearning:) – Archita patnaik Jun 20 '16 at 6:47

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.