0

I'm using a SOQL to query the lookup relationships.

Primary Object: Opportunity

Secondary Object: Purchase__c.

From purchase__c we have a lookup to opportunity.

Field Name: Opportunity__c

Child Relationship Name: Purchases2

SOQL query:

for (Opportunity opp : [select Id, Name, (select id from Purchases2__r where Count__c>0) from Opportunity where Id IN :oppIds]) 

whats wrong with my query? getting error below:

If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.

  • Are you certain that the error is about this particular query? I can't see anything wrong with your query given the information that you provided (provided it is all correct). – Derek F Sep 16 '16 at 18:26
3

Verify the API Name of your child relationship as follows:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationship())
    if (relation.getChildSObject() == Purchase__c.sObjectType)
        system.debug(relation.getRelationshipName());

Copy the output verbatim and use it where you have Purchases2__r.

  • 2
    Wish we had a useful code snippet library here on SFSE. Its little tidbits like these that keep being repeated that would be great to have outside of find the questions it is attached to.....but I guess there are other services for that.... – Eric Sep 16 '16 at 18:43
  • I think It will help if the documentation feature ever gets rolled out here. – Adrian Larson Sep 16 '16 at 18:52
  • I think I should add a resource like that to my library, but I'd need more than just 1 sample. – sfdcfox Sep 16 '16 at 21:26
0

Go to your child object that is in the subquery, go to the lookup field, check the child relationship name. This is the name you must use in your subquery along with the __r. Also make sure there isn't more than one field that looks up to the parent and that you are using the correct field for your logic.

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.