Skip to main content
1 of 4
ipavlic
  • 3.8k
  • 7
  • 34
  • 56

Map<Object, SObject> cannot be cast to Map<String, Account>

I'm trying to cast a Map<Object, SObject> to Map<String, Account>, which fails with the error

Incompatible types since an instance of Map<Object,List> is never an instance of Map<String,List>

That's because Apex likely gets transpiled to Java where Map is a compile-time abstraction. Unlike Java, we can't force the issue by writing something like

Map<Object, SObject> foo = ... // some initialization
Map<String, Account> bar = (Map) foo;

because raw Map doesn't exist in Apex. Can this be worked around somehow? The end goal is to have a Map<Object, SObject> return type which would allow a method to return maps of various types of SObjects.

ipavlic
  • 3.8k
  • 7
  • 34
  • 56