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
Arut JothiArut Jothi 

Map Collection of SObjects

Map values are not guaranteed to be unique (but their keys are) and this implies that (unlike Set) maps may contain duplicates. However, will a map of SObject ever have duplicates??

Consider map<Id, SObject> and let us assume that this map collection is obtained from a trigger context variable (such as newMap or oldMap). For example, let us take the example of a trigger over Account object: 
Map<Id, Account> accountMap = trigger.newMap;

Now let us further make an assumption that it was really a bulk trigger and there were roughly greater than 50 records. Specifically, containing two updates over the same record. Now, when the trigger context variables are populated by the salesforce platform, how does it handle these duplicate records (or 2 instances of same record)? Per my understanding is that Map keyset is guaranteed to be unique.
Best Answer chosen by Arut Jothi
Pradeep SinghPradeep Singh
Yes, Map contains unique values in key. If you have 2 or more records with same ID for update. Trigger.map will contains only one of them and that is the lst one as it overrides the previous one(having same ID).