0

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?

7

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.

  • Strangely, I am getting null in debug – salesforce-sas Aug 26 at 14:24
  • +1 nice way to get the exact ChildRelationshipName – Vijay Ganji Aug 26 at 14:24
  • +1 It is actually on child AccountContactRelation on which we are getting relationship field and not child Contact. Modified it, hope you dont mind – salesforce-sas Aug 26 at 14:28
  • Ah, yeah. Whoops. – Adrian Larson Aug 26 at 14:31
  • 2
    @sdfcadmdev Make sure you enabled Allow users to relate a contact to multiple accounts feature. And the API version of the class is set to the latest version. – Vijay Ganji Aug 26 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.