function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eager-2-LearnEager-2-Learn 

Script Statement tweaking

Hi,

 

I found this type of method in the Cook book area that helps remove script statements as I understand; however, I wanted to make sure that I understand the key in this type of code.

 

I wrote the looping version (not shown) and adding the MyCustomKey to the map as the key value but in the code below I thought if I put that field first in the SELECT statement it would pick that field but instead it looks like it automatically chooses the ID!  Is that a correct statement and if so is ther a work around to force it to select the key I want or do I have to stay with the loop method to get the results I want.

 

...

...

String DynamicQuery = 'SELECT MyCustomKey__c, Name, Id FROM Opportunity ' + WhereClause;
Map<String, Opportunity> OppsMap = new Map<String, Opportunity>((List<Opportunity>)Database.query(DynamicQuery));

...

...

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SRKSRK

you can get the output of a query in a map but the key element is always the ID there is no work around throw which you can make any other field as key

to make any other field as key to the map you have to use loop

 

map<id,account> accMap = new map<id,account>([select name,id from account]);

above query will fatch all the account & no matter in which order you write the field sequence in query it will always make Id as a key element for map

 

Thanks

SRK

All Answers

SRKSRK

you can get the output of a query in a map but the key element is always the ID there is no work around throw which you can make any other field as key

to make any other field as key to the map you have to use loop

 

map<id,account> accMap = new map<id,account>([select name,id from account]);

above query will fatch all the account & no matter in which order you write the field sequence in query it will always make Id as a key element for map

 

Thanks

SRK

This was selected as the best answer
Eager-2-LearnEager-2-Learn

That's what I thought but I need to double check with the experts! :)  Thank you for your support.

SRKSRK

Welcome :)