My method is being passed a list of objects. I want to use the object IDs in a where clause to retrieve a list of related objects. Representing this scenario abstractly:

`public methodname (List<inputObject> inputObjects) {
       List<otherOBject> otherObjects = new List <otherObject>;

       otherObjects = [select field1, field2, ... fieldn from otherObjects where lookupfield in (:inputObjects)];

In this case otherObject.lookupfield is a lookup to inputObject, so there is a relationship. Is apex/SOQL smart enough to use inputObject.ID to qualify this query? Or can I pass an array of Ids that I build from inputObjects? Or is there some other collection structure I should use for this filter?

1 Answer 1


For lookup fields, you can pass in a List<sObject> as a filter:

otherObject[] otherObjects = [SELECT field1, ... from otherObject WHERE lookup__c = :inputObjects];

Note that for Apex, = and in are synonyms if the bound parameter is a List (of sObject, Id, etc).

  • so it's easier than I thought -- thanks for the info -- can't wait to try it out. Aug 31, 2022 at 10:25

You must log in to answer this question.

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