function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ek0nomikEk0nomik 

Is it possible to query a lookup relationship (API)?

Let's say I have two objects: Product Serial Numbers and Returns.

 

Product serial numbers are not required to have a return, because, the product may be operating absolutely fine.  But, when a return is created, there is a lookup to the product serial number so that the user will know what specific product had the issue so that it can be inspected, taken our of service, etc.

 

When I am querying a list of product serial numbers, am I able to pull in the optional return records that are associated with it?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
SELECT Id, {other fields}, (SELECT Id, {other fields} FROM Returns__r) FROM Product_Serial_Number__c

In Apex Code, each Product_Serial_Number__c will have a List<Return__c> called Returns__r, which can be checked for emptiness (List.isEmpty()), number of records (List.size()), and can be iterated through like any other List<SObject>.

 

In {insert your language here}, the constructs may be different, depending on a number of factors, including if you used the Enterprise or Partner WSDL, the IDE and how it designs data consumption, and the language's interface with web services/SOAP.

All Answers

sfdcfoxsfdcfox
SELECT Id, {other fields}, (SELECT Id, {other fields} FROM Returns__r) FROM Product_Serial_Number__c

In Apex Code, each Product_Serial_Number__c will have a List<Return__c> called Returns__r, which can be checked for emptiness (List.isEmpty()), number of records (List.size()), and can be iterated through like any other List<SObject>.

 

In {insert your language here}, the constructs may be different, depending on a number of factors, including if you used the Enterprise or Partner WSDL, the IDE and how it designs data consumption, and the language's interface with web services/SOAP.

This was selected as the best answer
Ek0nomikEk0nomik

Thanks, that did the trick.  I'm not sure how I missed that in the documentation.