I've got a map whose values are a wrapper class. The wrapper class holds a custom sObject list, and the sObject in turn was queried to include related fields from a lookup relationship.

Problem is, when I use JSON.deserialize(jsonString, Map<Id, MapWrapper>.class), although the custom sObject is parsed, the related fields via the lookup relationship are not.

In prettified JSON, my serialized wrapper class (actually a map of ID to wrapper) looks like this, with the missing related fields highlighted :

         "Name":"Mohyee Holdings LLC"
            "LLC_BI__Source__c":"Tax Return",
               "LLC_BI__Type__c":"UCA Cash Flow",

After I run JSON.deserialize(jsonString, Map<Id, MapWrapper>.class) I get

{0011b00000Z6Pa8AAF = MapWrapper:[  
         Name=Mohyee Holdings LLC
         LLC_BI__Statement_Date__c=2018-12-31 00:00:00,
         LLC_BI__Source__c=Tax Return}

Note that the related sObject LLC_BI__Spread_Statement_Type__r is no longer included in the LLC_BI__Spread_Statement_Period__c object after deserializing.

Here's the wrapper class that JSON.deserialize() is casting to:

public class MapWrapper{
    @AuraEnabled public Account entity{get;set;}
    @AuraEnabled public list<LLC_BI__Spread_Statement_Period__c> periodList {get;set;}      

You are simply being tripped up by default logging behavior. Just because system.debug(someInstance) does not show you some attributes does not mean they are not set. Specifically, logging any SObject does not show you parent values. This behavior is easy to demonstrate.

Run this script in Execute Anonymous:

Map<String, Object> data = new Map<String, Object>
    'Account' => new Map<String, Object> { 'Name' => 'ACME' }
Opportunity record = (Opportunity)JSON.deserialize(
    JSON.serialize(data), Opportunity.class

Observe the logs:


The lesson here is simple. Do not use system.debug to verify behavior, especially of complex object attributes.

  • Well shoot, you're right. I found the error because when I tried to access the sObjects related field it was returning null. But when I deserialize the map and immediately try system.debug(entityPeriodsMap.values()[0].periodList[0].LLC_BI__Spread_Statement_Type__r.LLC_BI__Type__c); ... well then, sure enough, it does access it. Guess I haven't pinpointed the actual bug in my code, thanks Adrian! – smohyee Dec 5 '18 at 17:18

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.