Get list of one column SOQL
Get field values in SOQL
The simplest way to retrieve record IDs without a for loop is by using a Map and the keySet() method.
How to Obtain Unique Field Values Without Using a For Loop?
The simplest way to retrieve record IDs without a for loop is by using a Map and the keySet() method.
apex
Set<Id> accountIds = new Map<Id, Account>(
[SELECT Id FROM Account]
).keySet();But how can you obtain values for other fields?
You can accomplish this using field aliasing.
As shown on previous slide, a Map will automatically use the Id as a Map key.
Don't do this
Set<String> industries = new Set<String>();
for (Account acc : [SELECT Industry FROM Account]) {
industries.add(acc.Industry);
}Do this
Set<String> industries = new Map<String, SObject>([
SELECT Industry Id
FROM Account
WHERE Industry != NULL
GROUP BY Industry
]).keySet();To avoid Row with null Id at index: 0 always add null check (!= NULL)


