1

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.

2 Answers 2

5

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'));
0

.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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .