1

This question already has an answer here:

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?

marked as duplicate by Santanu Boral, Derek F, Adrian Larson Apr 27 '17 at 15:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.