[SalesForce] Didn’t understand relationship ‘AccountContactRelation’ in FROM part of query call

I'm getting the following error message:

System.QueryException: Didn't understand relationship 'AccountContactRelation' 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.

Select Email, Id, (Select ContactId, AccountId From AccountContactRelation) From Contact

In AccountContactRelation object there are two lookup fields for Account and for Contact.

Please advise how to avoid such error message?

Best Answer

You have to use the child relationship name. You can always find it by using a script like the following:

for (ChildRelationship relation : SObjectType.Contact.getChildRelationships())
    if (relation.getChildSObject() == AccountContactRelation.sObjectType)
        system.debug(relation.getRelationshipName());

Paste the relationship name verbatim into your subquery, rather than the child object name.

Notice that you have two FROM clauses:

Select Email, Id, (Select ContactId, AccountId From AccountContactRelation) From Contact

In your subquery, you need to use the relationship name. In your top level query, you need to use the API Name of that SObject. Change AccountContactRelation to AccountContactRelations and you query should compile.

Related Topic