SOQL
Get Ids
Use Map constructor Map<ID,sObject>(recordList) to get Ids from List of SObjects.
❌
Set<Id> accountIds = new Set<Id>();
for (Account acc : [SELECT Id FROM Account]) {
accountIds.add(acc.Id);
}
List<Account> accounts = [SELECT Id FROM Account];
Set<Id> accountIds = new Set<Id>();
for (Account acc : accounts) {
accountIds.add(acc.Id);
}
✅
Set<Id> accountIds = new Map<Id, Account>([SELECT Id FROM Account]).keySet();
List<Id> accountIds = new List<Id>(new Map<Id, Account>([SELECT Id FROM Account]).keySet());