1

My GC_Employee_Action__c object has two lookup relationships to the parent Employee__c object, eg: 1 lookup for employee and 2 lookup for his manager who also is an employee.

I have the following query :

Select Id, Name (Select Id, Close_Date__c from GC_Employee_Actions__r) From Employee__c 

While my Employee objects have actions (GC_Employee_Action__c) assigned to them I cannot retrieve these actions. Is there the possibility that the sub query does not work because of the double lookup relationship from GC_Employee_Action__c to Employee__c?

5
  • 2
    If you literally copy-pasted the query it's just a matter of putting a comma after Name: Select Id, Name, (Select Id, Close_.... The two child relationships will have different names, you can find them on the properties screen of the lookup field. You can use them both in queries. May 9, 2016 at 12:30
  • No I am sorry my fault. The syntax is correct I just forgot the , here.
    – Loannies
    May 9, 2016 at 12:41
  • In that case, look up the name of the child relationship that you actually want to query for, and make sure you have the proper name in your query. May 9, 2016 at 12:43
  • 1
    Let's save some time here. Add a screenshot of the lookup field definition and let us know the error you are getting.
    – Eric
    May 9, 2016 at 14:26
  • I am not getting an error, that is the problem. I wish I got. It just does not return records that i know they exist there. I have checked everything as far as names and relationships are concerned and not only me. I just hoped it has happened to someone else before and found a solution.
    – Loannies
    May 9, 2016 at 14:30

3 Answers 3

1

Inner query needs to be formated as field:

SELECT Id, 
   (SELECT Id FROM Contacts)
FROM Account

Since relationship is like field/param for SObject

1
  • No I am sorry my fault. The syntax is correct I just forgot the , here.
    – Loannies
    May 9, 2016 at 12:41
1

Actually in your query

Select Id, Name (Select Id, Close_Date__c from GC_Employee_Actions__r) From Employee__c 

Make sure that you have given proper relationship name "GC_Employee_Actions__r"

go to child object object definition page i.e ( GC_Employee_Action__c ) click on lookup field which u have created to Employee there you see what is the relation ship name then use that

1
  • The problem is that this is only a sub query of a much larger query and I have to go from parent to child. I tried everything in workbench it just does not work. Thank you for your answers!
    – Loannies
    May 9, 2016 at 13:25
1

To explain it perfectly lets assume i have two Account lookup relationship fields in Contact object.

enter image description here

So now i have 3 relationship fields in Contact which points to Account. Just like that, Account have 3 related lists of Contact. If you open up each of the Account relationship field in Contact you will get the following screens.

enter image description here

enter image description here

enter image description here

The following sub queries are valid

select id,(select id from contacts) from account
select id,(select id from contacts1__r) from account
select id,(select id from contacts2__r) from account

Note there is no __r reference in sub query for contacts as the relationships are standard and the other two are custom.

To come to your question, you should open the two relationship fields and use the appropriate child relationship name with __r.

Hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .