0
votes

the following Salesforce article says that:

When working with SOQL queries, maps can be populated from the results returned by the SOQL query. The map key should be declared with an ID or String data type, and the map value should be declared as an sObject data type.

Assume on the Account object I have a field of type text that is unique called uniquetext__c, how can this be achieved:

Map<string, Account> map_acc = new Map<string, Account>([select uniquetext__c, name, customField1, customField 2 from Account limit 10]);

My expectation is to have a map between uniquetext__c and the Account sObject, rather than the ID and the Account sObject

1

1 Answers

1
votes

Unfortunately, you can't set what field will be the key when constructing a Map from a SOQL query (AFAIK). But to do it manually all you have to do is...

Map<String, Account> map_acc = new Map<String, Account>();
for(Account a : [SELECT uniquetext__c, name, customField1, customField 2 from Account limit 10]){
    map_acc.put(a.uniqueText__c, a);
}