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
Micky MMicky M 

MAPS

Hi all anyone see what stupid mistake im making here?

 

Ive got a map:

 

map<string, Consultant__c> ConsultantMap = new map<string, Consultant__c>([select name, id from Consultant__c]); //this seems to work fine.


and all i want to do is use the value from a pick list as the key, so if i select 'Darth Vader' it uses that as the key and gets me the id.

 

now i've tried:

 

UsersId = ConsultantMap.get(pa.Login_As__c).id;

 

and even

 

Consultant__c X = ConsultantMap.get('Darth Vader');

system.debug('X' + X.Id);

 

but every time it fails and the id is null. yet the map is populated ok. I dont get what im going wrong.

 

Thanks All


Best Answer chosen by Admin (Salesforce Developers) 
hemantgarghemantgarg

I think you are looking for it :-

 

map<string, Consultant__c> ConsultantMap = new map<string, Consultant__c>();

 

for(Consultant__c con : [select name, id from Consultant__c]){

     ConsultantMap.put(con.Name, con);

}

 

now you can pass the string value as a key and get the Id of consultant.

All Answers

ngabraningabrani

The ConsultantMap that gets created has the following behavior

Key -- The id of the Consultant__c objects that are returned by the select query

Value -- The Consultant__c objects with two fields name and id.

 

If you have the id of the consultant you can use the map to get the corresponding Consultant object. The value you get from the picklist must be the Consultant id, then the code will work. Try to print the map using system.debug - it will help in understanding what are the key value pairs in the map. Also print the value that you are passing as input to the get method of the map.

Micky MMicky M

well this is whats in the consultand map

 

Name=Mick Murphy, Id=a1OM00000004CSYMA2}, a1OM00000004CSZMA2=Consultant__c:{Name=George Kane, Id=a1OM00000004CSZMA2}, a1OM00000004CSiMAM=Consultant__c:{Name=Ross Bisby, Id=a1OM00000004CSiMAM}, a1OM00000004CSxMAM=Consultant__c:{Name=Boba Fett, Id=a1OM00000004CSxMAM}, a1OM00000004CyOMAU=Consultant__c:{Name=B2netTest Survey Site Guest User, Id=a1OM00000004CyOMAU}, a1OM00000004CyPMAU=Consultant__c:{Name=Simon Dorling, Id=a1OM00000004CyPMAU}, a1OM00000004CyQMAU=Consultant__c:{Name=Alan Wooler, Id=a1OM00000004CyQMAU}, a1OM00000004CyRMAU=Consultant__c:{Name=Will Goulding, Id=a1OM00000004CyRMAU}, a1OM00000004CySMAU=Consultant__c:{Name=Mark Butcher, Id=a1OM00000004CySMAU}, a1OM00000004CyTMAU=Consultant__c:{Name=Simon Steele, Id=a1OM00000004CyTMAU}, ...}

 

so ive got a list of names and id's

 

I thought i could pass a string as the key value and get the id? is that not the case?

ngabraningabrani

If you take a look at a typical key-value pair. It says --

 

a1OM00000004CSZMA2=Consultant__c:{Name=George Kane, Id=a1OM00000004CSZMA2}

 

Here key is the id (a1OM00000004CSZMA2) and

 

Value is the Consultant object -- Consultant__c:{Name=George Kane, Id=a1OM00000004CSZMA2}

hemantgarghemantgarg

I think you are looking for it :-

 

map<string, Consultant__c> ConsultantMap = new map<string, Consultant__c>();

 

for(Consultant__c con : [select name, id from Consultant__c]){

     ConsultantMap.put(con.Name, con);

}

 

now you can pass the string value as a key and get the Id of consultant.

This was selected as the best answer
Micky MMicky M

Thanks so much guys, thats sorted it, it seems obviousl when someone points it out! Thanks All!!

Suresh RaghuramSuresh Raghuram

for(Consultant__c con : [select nameid from Consultant__c]){

     ConsultantMap.put(con.Name, con);

}

 

hi i did not understand what the above statement is doing are we creating our own ids or some thing else , can you explaing  what is happening with in the for loop ad put.

 

Thanks in advace.

hemantgarghemantgarg
This is just creating a map of consultants by names as key.