+ Start a Discussion

Internal Salesforce Error when inserting list<contact> on deserialized class instance



I recently had a test started rendering an Internal Salesforce Error without any code changes on my end.  I have managed to write a simple test class that demonstrates the issue.  Basically, this seems to be related to deserializing JSON to a class and then attempting to insert a list of Contacts in a variable on the class.


Here is a simple example which will render the error: 


public with sharing class InsertJSONContactList {
    public string someVar;
    public list<Contact> insertContacts;
    public InsertJSONContactList () {
        insertContacts = new list<Contact>();
        someVar = 'hello';
    public static testMethod void testSerialize() {
        // Instantiate the class
        InsertJSONContactList ext = new InsertJSONContactList();  
        // Make sure we are connected to reality
        // Add a contact to the list
        ext.insertContacts.add(new Contact(LastName='Rogerson', Email='rogerson@superfaketestemail.org'));
        // Insert the list, this will work
        insert ext.insertContacts;
        // Reset the list for later...
        ext.insertContacts = new list<Contact>();
        // Serialize the class
        string jsonExt = JSON.serialize(ext); 
        // Deserialize the string
        JSONParser parser = JSON.createParser(jsonExt);
        Type wrapperType = Type.forName('InsertJSONContactList'); 
        InsertJSONContactList nwExt = (InsertJSONContactList) parser.readValueAs(wrapperType); 
        // Make sure we are still connected to reality
        // Add a contact to our list
        nwExt.insertContacts.add(new Contact(LastName='Davidson', Email='davidson@superfaketestemail.org'));  
        // Insert the list. Internal Salesforce Error
        insert nwExt.insertContacts;  


This looks like a bug to me, especially since it only recently started happening after working fine for some months. However, I would love to hear of any suggestions for workarounds.  I will have to refactor quite a bit of code to get around this problem.

Forgot to mention that this is in a developer instance.

This passes in the new release.  Just hold on for one more week! :)

Great! Good to hear. Thanks :)