You need to sign in to do that
Don't have an account?

Lookup Relationship in SOQL
Dear All,
I would like to retrive data with the help of Lookup Relationship Field
1) I have Account Object---Account__c and created a Lookup field: Maincontact__c (lookup name is Maincontact__c)
2) Lookup value object is Contact : Contact__c and here we have child object for contact is Country: Country__c
3) here my question is i would like to Select the fields from Account, Contact and Country
Note: Account and Contact having only lookup relationship there is no parent and child relationship for Account and Contacts here
Can some one let me know how to retrive data based on my requirement.
Thanks,
Satis
I would like to retrive data with the help of Lookup Relationship Field
1) I have Account Object---Account__c and created a Lookup field: Maincontact__c (lookup name is Maincontact__c)
2) Lookup value object is Contact : Contact__c and here we have child object for contact is Country: Country__c
3) here my question is i would like to Select the fields from Account, Contact and Country
Note: Account and Contact having only lookup relationship there is no parent and child relationship for Account and Contacts here
Can some one let me know how to retrive data based on my requirement.
Thanks,
Satis
You can retreive the data using relationship name fields.
Say the relationship name fields as as
Account-- Contact --------Maincontact__r
Contact -- Country --------Country__r
Then you need to make query on Country object and can do the following.
The above SOQL will fetch the name and Id from all three objects.
Just replace the relationship names
Hope this helps.!!!!!!
Can you please provide me relationship names of lookup fields.
Lookup field Relationship Name is Maincontact__r for Contact
Object Lookup Filed Relationship Name
Account__c MainContact __c Maincontact__r
Contact__c
Child Object for Contact is Country__c
There is no parent and child relationship between Account to Contact via lookup field relationship we are fetching data for contact so for so good but to fetch data of child object of Country i couldnt make it
Thanks if you can help me
Thanks,
I tried but system is not able to understand the relationship why because there is no parent to child relationship for Account and Contact. through lookup relationship we could able fetch the data of contact so far so good but going forward to fetch data of country its not able to understand the relationship for system why becasue there is no direct relationship for Country and Account.
Hope you got my point!
Thanks,
To fetch the data from account while quering from contact you can user the below code. In above query account name will be fetched along with Contact emailId , Id and Name.
The relationship name is Account only.
Now if you have a relationship between Country and Contact as MainContact__r try the bwlow code
It should work.
If that is still not working check if the below one is working first.
If this works then above one should work as well. Let me know in case of any issue.
Thanks.
Have you try
Above all scenarios doen't working i hope We can not fetch the data wtih the help of lookup field relationship to child object data.
Again I am explaining you scenario and try from your system as well if you have this kind of relationship in your organization.
1) Account: is Master Object
Contact is an Object but there is no child relationship between Account and Contact
We have created lookup Field in Account object as Main contact in side the main contact lookup field we are showing the Contact Object.
2) Contact Object having child relationship with Country
Here Country is child of Contact
Contact to Country we have child relationship but Contact to Account having Lookup relationship.
hope you guys Clear, I have done all the queries which you guys provide and my self i hope you cannot call the data from look field to child relationship object data. this is what i got the conclusion.
Thanks for your suggestion.
Thanks,
SELECT Id, (SELECT Id From MainContact__c_FieldChildRelationshipName), Field childRelationshipName means what
this is query will not pass
(Select Id From SELECT IdFromContact__cFieldChildRelationShipName) From Contact
Where is the country and Account relatonship and why two times (Select Id From SELECT IdFromContact__c)
Have you tried before this kind of query in your organization?
Thanks,
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT Id, (SELECT Id From ChildRelationshipWithAccount), (Select Id From ChildrelationshipWithCountry) From Contact
--------------------------------------------------------------------------------------------------------------------------------------------------
- ChildRelationshipWithAccount :- Its the child relationship with Account and Contact which you will get on the "MainContact__c" field Defination page
- ChildrelationshipWithCountry:- Its the child relationship with Conutry and Contact which you will get on the "Contact__c" field Defination page of "country__c" Object.
FYI: When you create a lookup of any field its automatically create Parent-Child relationship.
You just need to put the right relationship name that is it
You can catch a screen show of scheema builder relationship and then I can assist you better.
Also Do following steps.
1. Go to country and find the Lookup field for Contact and see if I am using the correct one.
2. Account is already a parent of contact in salesforce so that should work as it is
If below query is working then above one will be fine.
Just see if the MainContact__r is the relaationship name and try the query in Developer Console first.
I think you are get confussed between Account- Contact Standard relationship and Contact-Account relationship which is created by Satis.
In you query, you are using Standard relationship fo Account and Contact where account is Parent and Contact is Child but In Satis' sObject Schema he is created another relationship between Account and Conatct in which Contact is Parent and Account is Child.
Correct me if I am wrong anywhere.!!
Any ways he can use your query with little modification as.
Note that I have used the plural label that is instead of "MainContact__r" ---- I have used "MainContacts__r" note the addition of "S" Just add the same relationship name of Country as you are done !!!!!!!!!!!!!!!!!!!!!!11
Thanks Shrikant for mentionng this.
Good Morning,
Hope you are doing well!
Okey I got your confusion Point here, forget about account and contact relationship in salesforce will take an new example with our names.
Below is the example:
1) Satis__c is an Object
2)Shrikant__c is also an another Object
3) Mudasir__c is also an Object
--------------------
Step1: Satis and Shrikant does not have any parent and child relationship
due to which i have created lookup field in Satis Object for to communicate Shrikant
l have given a lookup field name as "Mainshrikant__c" for lookup relationship name is Mainshrikant__r
Step2: Shrikant and Mudasir having Parent Child relationship, Mudasir is a child object under the Shrikat__c object.
Here my concern is i would like to get data from Satis table, Shrikant table and Mudasir table.
How do you want me to query for all the three tables in single query.
Note: Remember there is no parent and child relationship for satis and shrikant we have created lookup field relationship.
Thanks for your speedy response.
Thanks,
SELECT Id, (SELECT Id From Mainshrikant__r), (Select Id From childrelationshipName of Shrikant and Mudasir) From Shrikant__c
i am finding the below error
INVALID_TYPE:
SELECT Id,(SELECT Id From MainShrikant__r),(Select Id From
^
ERROR at Row:1:Column:27
Didn't understand relationship 'MainShrikant__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Thanks,
Above results from WorkBench and the below error from Developer console
SELECT Id,(SELECT Id From MainShrikant__r),(Select Id From
^
ERROR at Row:1:Column:27 Didn't understand relationship '
MainShrikant__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Thanks,
Could you please share defination page of this field not Object "MainShrikant__c" Field ?
Thanks!
Please find the above definition page
SELECT Id, (SELECT Id From Satis__r), (Select Id From Mudasirs__r) From Srikant__c
So far so good we can select the data from two table but i need to put where condition for specific field in Satis object and specific field in Shrikant object based on that i need to pull the data for all the 3 tables.
Thanks,,
---------------------------------------------------------------------------------------------------------
SELECT Id, (SELECT Id From Satis__r WHERE YOUR_CONDITION), (Select Id From Mudasirs__r) From Srikant__c WHERE YOUR_CONDITION
-------------------------------------------------------------------------------------------------
If its helps, please mark as best answer so it will help to other who will serve same problem.
Thanks!
and also filtering data for Mudasir and Srikant
i tried to put where condition with AND operator for the both buts its giving an error
I need a combined related data of all three 3 tables.
SELECT Id, Family__c,(SELECT Accounts__r.AccountNumber From Satis__r Where Accounts__r.AccountNumber = '123456'),(Select Platform__c From Mudasirs__r) From Shrikant__c Where Family__c ='Heaven'
SELECT Id, Family__c,(SELECT Accounts__r.AccountNumber From Satis__r Where Accounts__r.AccountNumber = '123456' AND Main_srikant__r.Family__c ='Heaven'),(Select Platform__c From Mudasirs__r) From Shrikant__c Where Family__c ='Heaven'
so its fetching all the family which is related to Heaven and on top of that its showing the account number which is having family and Platform.
Example: AccountNumber='123456' is having only one family with the name of Heaven and Platforms so for so good its fetching and also its searching all the family which i name Heaven.
here we need to restrict to show all the family with the name of Heaven. we have to show only with this account number associaed family with the name of Heaven
Thanks,
-----------------------------------------------------------------------------------------------------------------
List<Shrikant__c> lstShrikant = [
SELECT Id, Family__c,(SELECT Accounts__r.AccountNumber From Satis__r Where Accounts__r.AccountNumber = '123456' AND Main_srikant__r.Family__c ='Heaven'),(Select Platform__c From Mudasirs__r) From Shrikant__c Where Family__c ='Heaven'
];
List<Shrikant__c> lstRequiredRecords = new List<Shrikant__c>();
for(Shrikant__c record : lstShrikant){
if(record.Satis__r != null && record.Satis__r.size() > 0){
lstRequiredRecords.add(record);
}
}
// Finally you get required result . in "lstRequiredRecords"
--------------------------------------------------------------------------------------------------------------------
let me know if helps!!!!!
Thanks!
--------------------------------------------------------------------------------------------------------------------
SELECT Main_srikant__c, Main_srikant__r.Family__c,Accounts__r.AccountNumber ,(Select Platform__c From Main_srikant__r.Mudasirs__r) From Main_srikanth__c Where Accounts__r.AccountNumber = '123456' AND Main_srikant__r.Family__c ='Heaven'
--------------------------------------------------------------------------------------------------------------------
FYI:
I never try, just let me know what happens with this query.
I am finding below error!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INVALID_TYPE:
Main_srikant__r.Mudasirs__r) From
Main_srikanth__c Where Accounts__r.AccountNumber = '123456'
^
ERROR at Row:1:Column:167
sObject type 'Main_srikanth__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks,
https://youtu.be/9ltnHYQ1tqE
https://youtu.be/3ULokr4yaEE