We realized that often time in our code we're iterating through a list of SObjects and creating a map of String to SObject. To try and reduce how many times we're repeating this code, we were going to add it to a generic SObjectDomain that specific SObject domains extend.
Map<String, SObject> createStringFieldToSObjectsMap(String fieldName, List<SObject> sobjects){
Map<String, SObject> sobjectMap = new Map<String, SObject>();
for(SObject currentSObject : sobjects){
sobjectMap.put(currentSObject.get(fieldName),currentSObject);
}
return sobjectMap;
}
The problem we're running into is that it's difficult to convert a generic SObject map into a specific SObject map. I tried just casting the generic map as the specific map, but ran into the following error:
Map<String, SObject> genericMap = new Map<String, SObject>();
Map<String, Account> accountMap = (Map<String, Account>) genericMap;
System.TypeException: Invalid conversion from runtime type Map<String,SObject> to Map<String,Account>
I also tried to declare the map with a dynamic SObject type in the following ways, but each time ran into an error saying that it was an invalid type.
Map<String, Account.SObjectType> accountMap = new Map<String, Account.SObjectType>();
Map<String, Account.SObjectType> accountMap = new Map<String, SObject>();
Map<String, Schema.SObjectType.Account> accountMap;
I also tried to use method like getSObjectType(), but I kept getting the following error:
Map<String, Account.getSObjectType()> accountMap = new Map<String, Account.getSObjectType()>();
Map<String, Account.getSObjectType()> accountMap = new Map<String, SObject>();
//Unexpected token '<'.
I found a lot of posts stating that dynamically determining SObject type wasn't supported, but these posts were at least 4 years old and a lot has changed in that time. Does anyone know if what we're trying to do is possible?
Best Answer
I am not pretty sure why you wanna do it. You can dynamically instantiate Map using Type.newInstance(), Probably that's what you need?
Then to test
Cast to AccountMap:
Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_type.htm