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
SunnyShinySunnyShiny 

unexpected token:ContactList.id soql query

Hello

I try to query a query with ID in (list of ids)

 

So i have my list with ids, name ....fields.

List<objectABC> myList = [select  relatedContact__r.id from objectABC];

 

and I need to manage limits and bulkify to create another list of contact with thus ids.

So I try >

List<Contact> lstContact = [select name, adress from contact where Id in :myList.id];

 

But I've got the error

Compile Error: Initial term of field expression must be a concrete SObject: LIST<objectABC>

 

 

How can I do that ?

Thanks so much for your help

Rakesh BoddepalliRakesh Boddepalli

Post code snippet that is causing the error, that would be easier to help you . 

Vincent.IpVincent.Ip

if you're trying to filter a soql query by the Id's of another list of objects, Apex allows you to just use that list of sObjects.  It seems to be a special case.  (Normally you'd have to create a list<id> variable to hold the list of Id's you want to filter on.)

 

 

so taking your example, you should be able to do the following.

 

List<objectABC> myList = [select  relatedContact__r.id from objectABC];

List<Contact> lstContact = [select name, adress from contact where Id in :myList];

 

 

Mayank_JoshiMayank_Joshi

Salesforce doesn't allow you to perform Below approach :

>>

    List<objectABC> myList = [select  relatedContact__r.id from objectABC];

 

    List<Contact> lstContact = [select name, adress from contact where Id in :myList.id];

 

For this ,we have to modify our approach as below :

>>

    List<objectABC> myList = [select  relatedContact__r.id from objectABC];

    Set<Id> st_contact = new Set<id> ();    

   

    For(objectABC objABC :myList)       

     st_contact.add(objABC.relatedContact__c);

 

   List<Contact> lstContact = [select Id,name, adress from contact where Id in :st_contact];

 

The reason, I have used relatedContact__c instead of  relatedContact__r.id (becoz ,Lookup Id can also be retrieved by this way ) .

 

Warm Regards,