You need to sign in to do that
Don't have an account?
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
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
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.
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?
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}
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.
Thanks so much guys, thats sorted it, it seems obviousl when someone points it out! Thanks All!!
for(Consultant__c con : [select name, id 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.