Is it possible to convert a sobject to a map?
The sObject has a native function getPopulatedFieldsAsMap that converts from an sObject to a Map . This was released in Summer ’16 which is probably why the other answers resort to using JSON serialization to accomplish this. It’s doable if you de/serialize to JSON.
Which is an example of a sobject key?
A map key represents a unique value that maps to a map value. For example, a common key would be an ID that maps to an account (a specific sObject type). This example shows how to define a map whose keys are of type ID and whose values are of type Account. Map m = new Map ();
How to create a map of sobjects in apex?
Within the curly braces, specify the key first, then specify the value for that key using =>. This example creates a map of integers to accounts lists and adds one entry using the account list created earlier. Maps allow sObjects in their keys.
What kind of data type do you use for sobjects?
Typically, you use the specific sObject data type, such as Account for a standard object or Book__c for a custom object called Book, when working with sObjects. However, when you don’t know the type of sObject your method is handling, you can use the generic sObject data type.
Can a map < string, sobject > be an object?
The problem is, even though this is an entirely realistic scenario, it isn’t one recognized by the Apex compiler, which will reject: Incompatible types since an instance of Map is never an instance of Map
Can a map be an instance of an object?
Incompatible types since an instance of Map is never an instance of Map This really doesn’t make much sense since I would expect every String to be an Object and every SObject to be an Object, and therefore I would expect every Map to be an instance of Map .
How to convert a sobject to a string?
The method which calls this method needs to convert an instance of Map to doSomething () and, if/as/when doSomething () works as expected, we can expect all the keys will be String and all the values will be SObjects.