2

I have one custom object which related to Account. one lookup field in Custom object which is to Account.

SELECT Id, Name, (SELECT id FROM Question_Answer__r) FROM Account

But this is showing don't understand relationship. Can anyone please help me

1
  • May be your child-relationship name is different that you use. For getting child relationship name, refer the answer that I gave. Hope it resolves your problem. Commented Jun 27, 2016 at 6:52

3 Answers 3

2

So you have a object, let se object1__c and it have a Account lookup (lets say Account__c).

In this scenario

Account - Parent Object1__c - Child

So your SOQL query should be like this

SELECT Id, Account__c, Account__r.Name, Account__r.Description FROM Object1__c

Updates

So if you want to query child object i.e Question_Answer__c

SELECT Id, Name, (SELECT Id, Name FROM Question_Answers__r) FROM Account

Get All Child objects relationship name.

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship cr: R.getChildRelationships()) 
{
  system.debug('====child object==='+cr.getRelationshipName());
}

Try to run above describe call in developer console and check the debug log and check the correct relationship name for Question_Answer__c object

8
  • I need to query from Account
    – user24737
    Commented Jun 27, 2016 at 6:40
  • @chris check my updated answer. If you don't know the correct relationship name then use above describe call to get the relationship name.
    – Ratan Paul
    Commented Jun 27, 2016 at 6:48
  • That is what i tried right. In my question for the same query system don't understand relationship. Your first query is working fine but second query is not working
    – user24737
    Commented Jun 27, 2016 at 6:48
  • 1
    Excellent! with your code i queried child relations. And i found that names child name was different. Thanks Ratan
    – user24737
    Commented Jun 27, 2016 at 6:54
  • 1
    Great explanation @Ratan :) Commented Jun 27, 2016 at 6:57
0

Actually in your query you need to use Child-Relationship name of object.You can find this in the lookup of Account on Question_Answer__c object.For ex. On contact there is an lookup to account.On this you will see the child relationship name is contacts.

enter image description here

So if you query from account it would be like this:

Select Id, (Select id from Contacts) from Account
0

You can access the account from the custom object. In your case it is Question_Answer__c.

You can write below query for this:

SELECT id, Account__c, Account__r.name from Question_Answer__c.

Note: Here Account__c is the api name of Account lookup, that you use in Custom object and Account__r denotes the relation name.

You can find out the relationship name, when you click on field mentioned in custom object. Below page is open.

enter image description here

Hope it helps you :)

2
  • I want to know how many Question_Answer__c records related with each account. So i would like to query from Account
    – user24737
    Commented Jun 27, 2016 at 6:43
  • In that case your query is right, but you need to check the relationship name. I think that relationship name is not Question_Answer. You need to check that name after click on lookup field. Commented Jun 27, 2016 at 6:47

You must log in to answer this question.