Cannot deserialize serialized object with Sub query
Last updated 2019-12-06 ·Reference W-6615092 ·Reported By 9 users
When two objects have relationship fields to each others, the query results of a SOQL query with sub query is not deserialized successfully in Apex. The error seen will be "Cannot deserialize instance of <unknown> from FIELD_NAME value totalSize or request may be missing a required field".
1. Create a Custom Object Usage (Usage__c)
2. Create a lookup field to Account
Field Label : Account ID
Object Name : Usage
Field Name : Account_ID
Data Type : Lookup
API Name : Account_ID__c
Child Relationship Name : Usage
3. On Account, create a lookup to Usage__c
Field Label : Usage
Object Name : Account
Field Name : Usage
Data Type : Lookup
API Name : Usage__c
Child Relationship Name : Accounts
4. Execute following code via dev console
Savepoint sp = Database.setSavepoint();
Account a = new Account(Name = 'test acct');
insert new Usage__c(Account_ID__c = a.id, Name='123');
List<Account> accts = [select Id,(select Id from Usage__r) from Account where Id = :a.Id];
// This will fail
String acctStr = JSON.serialize(accts);
System.debug('attempting to deserialize acctStr - this will fail '+acctStr);
System.debug('success at deserializing acctStr');
Actual Results :
Error : "Cannot deserialize instance of <unknown> from FIELD_NAME value totalSize or request may be missing a required field"
Expected Results :
Code should run successfully
An attempt to resolve this issue in Spring '20 had unintended side effects and had to be rolled back, with the corrected fix deferred to Summer '20. We apologize for the inconvenience.
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.