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
SalesRedSalesRed 

How To Create A Set of IDs from a SOQL Retuned List Without First Traversing List?

Hello,

 

I've come across the following scenario.

- I use a SOQL statement to get a list of records.

- I need to use the ID's from this list for a second subsequent query.  

 

Is there a way of getting all the ID's returned from the first query without looping through/travershing the result list first to get the ID's?

I don't wish to write a second query just to obtain the ID's but wondered could they alone be extracted from the first list without looping through the results?

 

Thanks for your help

Best Answer chosen by Admin (Salesforce Developers) 
Satish_SFDCSatish_SFDC
This should help.
You initialize the Map within the query

Map<ID, Lead> leadMap = new Map<ID, Lead>([SELECT Id, FirstName, LastName FROM Lead]);

Regards,
Satish Kumar

All Answers

Satish_SFDCSatish_SFDC
This should help.
You initialize the Map within the query

Map<ID, Lead> leadMap = new Map<ID, Lead>([SELECT Id, FirstName, LastName FROM Lead]);

Regards,
Satish Kumar
This was selected as the best answer
SalesRedSalesRed

Thanks for your help!

Satish_SFDCSatish_SFDC
You are welcome.