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
astroastro 

How to select what becomes the key for a Map Object

I have a map object being created as follows:

 

Map<Id,Customer_Lookup_Nov__c>([select id, id_Nov__c, Phone_Nov__c, Parent_ID_Nov__c, NOV_ID_Nov__c, MII_Name_Nov__c, Customer_Type_Nov__c, Customer_Record_Type_Nov__c from Customer_Lookup_Nov__c where id_Nov__c IN :accIds]);

 

This makes the Key of the Map the Customer_Lookup_Nov__c id.  I need the Key to be the Customer_Lookup_Nov__c.id_Nov__c value which I am returning in that soql query.

 

Is there any way to do this?

 

I've been trying to iterate over a SET of key's taken from the Map's keyset but then I can't 'get' a particular value from the set.  example: keySetVariable.get(i);  

 

set's do not have getter methods from what I've read in the apex reference guide.

 

Thank's again for the help. 


 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

 

Map<String, Customer_Lookup_Nov__c> mapupdate = new Map<String, Customer_Lookup_Nov__c>(); for(Customer_Lookup_Nov__c novmap:[select id,id_Nov__c, Phone_Nov__c, Parent_ID_Nov__c, NOV_ID_Nov__c, MII_Name_Nov__c, Customer_Type_Nov__c, Customer_Record_Type_Nov__c from Customer_Lookup_Nov__c where id_Nov__c IN :accIds]) { if(novmap.id_Nov__c != null) mapupdate.put(novmap.id_Nov__c, novmap); }

 This should do it I think...

All Answers

BritishBoyinDCBritishBoyinDC

 

Map<String, Customer_Lookup_Nov__c> mapupdate = new Map<String, Customer_Lookup_Nov__c>(); for(Customer_Lookup_Nov__c novmap:[select id,id_Nov__c, Phone_Nov__c, Parent_ID_Nov__c, NOV_ID_Nov__c, MII_Name_Nov__c, Customer_Type_Nov__c, Customer_Record_Type_Nov__c from Customer_Lookup_Nov__c where id_Nov__c IN :accIds]) { if(novmap.id_Nov__c != null) mapupdate.put(novmap.id_Nov__c, novmap); }

 This should do it I think...

This was selected as the best answer
ShikibuShikibu
So essentially, Map< , sObject> = [select id, ...] always returns a map from id to sObject, but you can rework it by replacing the keys with whatever you need.