You need to sign in to do that
Don't have an account?
Strange problem with Map.
Hi all,
I have a strange problem: I have map as:
Public map<Sobject__c,String> calresult{get;set;}
which i am initialising in constructor, and when values a inserted into a textbox i am putting them in my map,
But problem is this when i want to display it, I use system.debug() to see the map is filled or not, Its filled but when i am tring to fetch the first map value as: calresult.get(object); it always return null,
1.The value is in the map
2.No values are removed form the map
But it always return null .Help!
------------------------
Also i found that the first value is not equal to my object as one field which is numaric with value 12.0 is 12 inside my map, but my map is same type as of the object.?
I tried this simple code below:
When the field value in the sobject is changed, the map treats it as a different key.
Regards,
Hengky
All Answers
in maps from winter13 itself we have the ability to set the key value as sobject .....so i think you are desinging the class less than 26 api version.....
Thanks but it : 26.0 already
The 12.0/12 different could be the reason why you keep getting null. I think maybe in the background the map is using hash of the field values as the key to search for the object?
Why would you need a map of SObject as the key anyway?
Regards,
Hengky
Hey Neao18,
I had faced the same issue . I searched a lot but could not something feasible .
If you want to put the Key only to fetch the value in the Map.
You can make a map <String,String> then put the Sobject__C.Name as key then the value . You will get the value associated to the key
Thanks
Ankit
I tried this simple code below:
When the field value in the sobject is changed, the map treats it as a different key.
Regards,
Hengky
Salesforce uses hashes to index its associative collections and the hash is based on the object content not the address of the object. So by changing the fields you are changing the object’s hash, hence the object is no longer found in the map.
You can override this by implementing your own wrapper object that exposes the hashCode method. Alternatively you could index by the object Id or another field value.
- Andy