2

I have sets of objects CustomObjectA, CustomObjectB, CustomObjectC... that I need to pass to a method.

I would like to put them in a Map of type Map< String, Set< sObject > >, but when I try, I get an error:

Incompatible types since an instance of Set<CustomObjectA> is never an instance of Set<sObject>.

Incompatible types since an instance of Set<CustomObjectB> is never an instance of Set<sObject>.

When I change it to lists instead of a sets (using Map<String, List<sObject>>) and lists of my custom objects, I don't encounter the same problem.

Why would that occur?

moth
  • 454
  • 4
  • 19

1 Answers1

3

I explained a bit in this question that collection type casting is broken. You can't use Set<ConcreteSObjectType> if you want to be dynamic, so you'll have to settle for adding the records directly to a Set<SObject> instead.

sfdcfox
  • 489,769
  • 21
  • 458
  • 806