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
PERFECTIONISTPERFECTIONIST 

How to Retrieve data from Lookup Relationship object of Child Object

Dear All,

I would like to retrive the data from Lookup Relationship Object A1 and Child Object Data of B2 fields 
Example:  Account --Object Name
                   Main Contact: Lookup Name
                   Contact Object is Havng child Relationship with Country Object
Now my Question is I would liket o Fetch Account fields Data, Contacts Field Data and Country Object Field Data

Here is my planned query:

Select Name, MainContact__r.Country__r.Name FROM Account__c

as per my above query its not allowing me to write lookup to child object relationship

Can some one help me?


Thanks & Regards,




 
ManojjenaManojjena
Hi Satish ,

In salesforce SOQL we can query parent to child and child to parent .
From parent to child we can query with inner query with child relationship name .

From child to parent we can query with dot notaion .

Please check the below link .

https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

Let me know if it helps .

Thanks
Manoj
PERFECTIONISTPERFECTIONIST
Dear Manoj,

Thanks for the reply,

as i mentioned above here  am not worrying about Parent to child and Child to Parent relationship.

I need to fetch data from Lookup relationship to Child Object object relate data.

Let me Explain you in detailed i Have Created Account Object and added on filed with Main contact and given Lookup relationship for Contact. Under the Contact section i have created Child Relationship with another object called Country.

Now i need to fetch the Country Column Information, Contact Coulmn information and also Account column information.

Note: Account to Contact there is no child relationship i have added field with lookup relation as i mention above.

Thanks,
 
varun_vatsavarun_vatsa
Hi Sathish,

As mentioned lookups are relationships only in terms of salesforce. So as you have written a query that will work in scenario when you are trying to fetch parents data from the child record.

Object A could have multiple Contacts c1,c2,c3 so if you will try to fetch data using "DOT" notation you need to fetch from child to parent (Select one specific child and then its parent, if you do otherwise the compiler will get confused and will not be able to find which child record you want to refer to via dot notation.

So your Query might look something like this... 

Select Name , Contact__r.Name, Contact__r.Account__r.Name from Country__c;
Shrikant BagalShrikant Bagal
Hello Sathish,

Relation between object:
Parent Object             ChildObject
Contact =>                  Account with Lookup field (MainContact__c)
Contact =>                  Country__c with Lookup field (Contact__c)

Am I right?

Thanks!

 
PERFECTIONISTPERFECTIONIST
Dear Shrikant,

Parent Object is "Account" with Lookup field (MainContact__c) the Main Contain Lookup values are Contact Object
if you Click on Contact Object inside Child Object is Country

Thanks,


 
Shrikant BagalShrikant Bagal
Please try:
Select Name , Contact__r.Name, Contact__r.Account.Name from Country__c;
 
PERFECTIONISTPERFECTIONIST

Dear Shrikant,

Hope you are doing well!

a bit confusing what do you mean by "Contact__r" is this Lookup relationship Name of the field MainContact__c or its Contact Objact relationship name?
Account__c is Master Object---MainContact_c is the lookup field of Contact__c Object under the conact Object i have child relationship of Country__c Object 

Now i need to get the field data of all the object |Account__c |Contact__c|Country__c|

How do you want met o query


Thanks & Regards,