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
neao18neao18 

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.?

Best Answer chosen by Admin (Salesforce Developers) 
Hengky IlawanHengky Ilawan

I tried this simple code below:

 

Map<Account, String> accMap = new Map<Account, String>();
Account acc = new Account(Name = 'John Doe');
accMap.put(acc, 'The Account');

System.assert(accMap.get(acc) != null);

// now change the account name
acc.Name = 'John Smith';

System.assert(accMap.get(acc) == null); // <--- here searching the map using acc as the key returns null
System.assertEquals(accMap.size(), 1);

// now change it back
acc.Name = 'John Doe';

System.assert(accMap.get(acc) != null);
System.assertEquals(accMap.size(), 1);

 When the field value in the sobject is changed, the map treats it as a different key.

 

Regards,

Hengky

All Answers

kiranmutturukiranmutturu

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.....

neao18neao18

Thanks but it : 26.0 already

Hengky IlawanHengky Ilawan

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

amidstcloudamidstcloud

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

 

Hengky IlawanHengky Ilawan

I tried this simple code below:

 

Map<Account, String> accMap = new Map<Account, String>();
Account acc = new Account(Name = 'John Doe');
accMap.put(acc, 'The Account');

System.assert(accMap.get(acc) != null);

// now change the account name
acc.Name = 'John Smith';

System.assert(accMap.get(acc) == null); // <--- here searching the map using acc as the key returns null
System.assertEquals(accMap.size(), 1);

// now change it back
acc.Name = 'John Doe';

System.assert(accMap.get(acc) != null);
System.assertEquals(accMap.size(), 1);

 When the field value in the sobject is changed, the map treats it as a different key.

 

Regards,

Hengky

This was selected as the best answer
Andy Freeston_LarterAndy Freeston_Larter

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