14

When the new Apex type methods were added in Summer '12, it was possible to do

Type t = Type.forName('MyClass');
MyClass newObj = (MyClass)t.newInstance();

What is the equivalent when I want to instantiate a List of such a class for which I only know the name at runtime?

I am also trying to create multiple records using JSON.deserialize. I think the answer to the earlier question may also help me with creating a List of such objects with JSON.deserialize(jsonStringOfSeveralObjects, appropriateListClass).

Note: I am able to use a Type method to create a record of a custom object like this:

Type oType = Type.forName(‘MySobjectName’);                                           
sObject rec = (sObject) JSON.deserialize(jsonString, oType);

But when the JSON string is an array of objects I am unable to use a method similar to the example shown below:

List<InvoiceStatement> deserializedInvoices = 
    (List<InvoiceStatement>)JSON.deserialize(JSONString, List<InvoiceStatement>.class);

Can you suggest the right format to use with List<> to create multiple records? I am specifically looking for corresponding substitutes for List<InvoiceStatement> and List<InvoiceStatement>.class that can be dynamically determined.

1 Answer 1

19

The trick is to deserialize into a List<SObject>. This works for me:

String jsonString = '[{"Name":"A1"},{"Name":"A2"}]';
Type t = Type.forName('List<Account>');
List<SObject> lo = (List<SObject>)JSON.deserialize(jsonString, t);
for (SObject o : lo) {
    System.debug(o.getSObjectType().getDescribe().getName() + ', Name: ' + o.get('Name'));
}

Output:

Account, Name: A1
Account, Name: A2
8
  • 2
    Got this question by email - it was too good not to post!
    – metadaddy
    Sep 10, 2013 at 17:45
  • 3
    You can serialize into a subtyped list, as well. I tested this using: Type t = Type.forName('List<Account>'); Account[] x = (Account[])JSON.deserialize('[{"Name":"Test"},{"Name":"Test 2"}]', t);
    – sfdcfox
    Sep 10, 2013 at 19:14
  • 1
    Apex classes work differently from sobjects? That is weird. I'll alert the authorities...
    – metadaddy
    Feb 7, 2014 at 15:37
  • 1
    @e-bacho 2.0, I am not seeing the same issue you are seeing. I do notice a typo (missing the last "s" on Type.forName('List<WrapperClas>')), but that may be a typo here rather than copy paste of a typo in your code. Could you provide more detail? Type t = Type.forName('List<ICheck>'); system.debug(t); 11:17:11:059 USER_DEBUG [3]|DEBUG|LIST<ICheck> Feb 7, 2014 at 19:31
  • 1
    Thanks @metadaddy! @Josh, when you have inner class in your code and a list of objects from that class, it seems like you cannot get the type if you use the forName method - e.g. Type t = Type.forName('List<YourInnerClass>') will not work. It only works when you use the Type property class but you need to make sure you don't add whitespace character - Type t = List <YourInnerClass>.class will not work (note the space between List and <YourInnerClass>. Instead the only way it works is using the .class property without a whitespace character - Type t = List<YourInnerClass>.class Feb 9, 2014 at 22:25

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.