I'm looking for a slick way to convert an inner class to a map so I can dynamically reference its properties.

Specifically I'm looking to do something like (Sobject)myObj.get('some property') only myObj is an inner class not a concrete sobject.


You should consider using JSON.serialize() and JSON.deserializeUntyped(). When used together, you can create a Map from your Class. I have verified that this works:

public class OuterClass {
    public class InnerClass {
        public String type;

OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.type = 'quack';

String JSONInner = JSON.serialize(inner);
System.debug('Inner: ' + JSONInner);

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(JSONInner);
System.assertEquals('quack', m.get('type'));

.get() method can only be used for variables of type sObject or few standard data type. Custom class type cannot have a dynamic get method.

I am not sure why , but may be sObject type acts like a Map but a class doesn't.

in my opinion what you are doing is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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