Quick and easy way to convert a List<sObject> to Set<Id>

A quick and efficient way to convert a list of sObject’s into a Set of Id’s.

List<Id,Account> accountList = new List<Id,Account>();
accountList = [SELECT Id, Name FROM Account WHERE Name LIKE 'A%'];

//Convert list to Set of Ids.
Set<Id> accountdIds = (new Map<Id,Account>(accountList)).keySet();

Leave a Reply

Your email address will not be published. Required fields are marked *