[SalesForce] How to find the child relationship name

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.

Best Answer

Verify the API Name of your child relationship as follows:

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

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

Related Topic