1

I have a SalesForce custom object named Loan__c which has a Lookup field called LoanLead__c and I'm trying to query that relationship with SOQL but I'm getting an error when I run the query SELECT Id, (SELECT Id FROM LoanLeads__r) FROM Loan__c on the object in the developer console query editor.

I've confirmed the Object API name of the related LoanLeads table is indeed LoanLead__c and I have tried modifying the query to select from the LoanLead__r table and a few others, but I get the same error.

The error I'm getting is:


SELECT Id, (SELECT Id FROM LoanLeads__r) FROM Loan__c
                           ^
ERROR at Row:1:Column:28
Didn't understand relationship 'LoanLeads__r' 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.

This is what the field settings are for the lookup field: enter image description here

I can't figure out what I'm doing wrong - everything I've read says this should work and it does work with other lookup fields on this same table. Any help would be greatly appreciated.

2
  • 1
    If Loan__c has a lookup to LoanLead__c then LoanLead__c is the parent. Meaning the query would be Select Id, (select Id from Loans__r) from LoanLead__c. Be sure the relation name is Loans for this to work Apr 24, 2023 at 22:29
  • Yeah, that is what ended up working. SELECT Id, (SELECT Id FROM Loans__r) FROM LoanLead__c was what I needed. Apr 25, 2023 at 21:05

3 Answers 3

1

The Child Relationship Name is Loans, so it should be Loans__r, not LoanLeads__r.

1
  • Well, not only that but I didn't realize the relationship was from LoanLead__c to Loans__c instead of the other way around. What ended up working is SELECT Id, (SELECT Id FROM Loans__r) FROM LoanLead__c instead. Apr 25, 2023 at 21:05
1

As Peter Noges suggested, the parent table was actually the LoanLead__c table and not Loan__c so what ended up working for me was SELECT Id, (SELECT Id FROM Loans__r) FROM LoanLead__c and also SELECT Id, LoanLead__r.Id FROM Loan__c.

0

When we query child object in sub-query then we use Child Relationship Name. If relationship is custom then we need to append suffix __r.

Select Id, Name, (Select Id, Name From Contacts__r) from Account
Select Id, Name, (Select Id, Name From Contacts) from Account

When we query parent object in that case we use field name with suffix __r instead of __c.

Select Id, Name, Account.Name, Custom_Account__r.Name From Contact

Custom Relationship Field with Custom Relationship

Standard Relationship Field with Standard Relationship

I hope this will help you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .