I am trying to query records and collect them in a Set as below
Set<abc__c> a = [select id from abc__c limit 2];
but getting the following error Illegal assignment from LIST to SET.Can't I collect the result of a query in Set ?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityThere is simple way:
Set<Account> acc = new Set<Account>([SELECT id from Account]);
You cannot type cast a List<sObjectType>
to Set<sObjectType>
just by query assignment, pass it to Set
constructor instead.
//Fetching all account in map
Map<id,abc__c> aMap = new Map<id,abc__c>([select id from abc__c limit 2]);
Set<Id> a=aMap.keyset();
You cannot directly collect in set and using maps save script statements and avoiding loops .