List.Contains Salesforce Bug

Today I ran into an Apex issue using contains on a List<Id>s. Contains was returning false when the list definitely contained the Id. I contacted Salesforce support and they confirmed it’s a Salesforce bug. Their suggested workaround was to use a List<String> instead of List<Id>. In my case, I used a Set<Id> instead and that worked. In most instances, a Set<Id> makes more sense too because you don’t have to deal with potential duplicate Ids in the collection. It may be more performant too but it depends on the implementation of contains and the data structure’s implementation. A blog post for another day….

They said they’re working on it but have no timeline for a bug fix release. I also asked them to create a known issue since I didn’t find one but that’s not to say their isn’t one.

Run the following Apex script in execute anonymous to view the bug. I haven’t tried the script with other element types so I wonder if a List<Object> also has issues or if other List functions suffer from the same issue. Anyone have any info on that?

Leave a Reply

Your email address will not be published. Required fields are marked *