1

I am excited to getPopulatedFieldsAsMap that being released on Summer 16 . I tried to run query however some of field does not return value. I thought it was bug but somehow it should be fixed in Spring 17 because now already Summer 19.

I have some quick testing and find out that if I limit the query, I will able to see the field value. I am not sure if there any documentation on size of fields return limitation.

Here is my sample code :

/**  certain field fail to return value  
quote = [SELECT Id, Name ,Owner.Name,RecordType.Name, Date_Submitted__c , Date_Approved__c , Actual_Final_Approver__c , Primary__c , Status__c,
            Opportunity__c,Opportunity__r.Name,Account__c,Account__r.Name,SalesRep__c,Pre_Sales__c,Legal_Entity__c,
             Quote_Name__c,Quote_Summary__c ,Discount_Reason__c FROM Quote__c  
             WHERE Id = :ApexPages.currentPage().getParameters().get('quoteId')];

 */
 //this will return value 
 quote = [SELECT Id, Discount_Reason__c FROM Quote__c  
             WHERE Id = :ApexPages.currentPage().getParameters().get('quoteId')];

   mapQuote = quote.getPopulatedFieldsAsMap();        
   system.debug('@@@ map size '+ mapQuote.size());//map size only 20 ??? 

   //this will fail if I used 
   System.assert(mapQuote.ContainsKey('Discount_Reason__c'), 'Discount_Reason test');
   for (String fieldName : mapQuote.keySet()) {
        System.debug('@@@ field name is ' + fieldName + ', value is ' + 
            mapQuote.get(fieldName));
    }   

I am using class with version 46.

Thank you

4

What I feel is Discount_Reason__c would be null and hence it was not part of mapQuote.

I quickly wrote a test class to test this behaviour.

Account a = new Account();
a.name = 'TestMapAccount1';
insert a;
a = [select Id,Name,Type  from Account where id=:a.Id];
Map<String, Object> fieldsToValue = a.getPopulatedFieldsAsMap();

System.assert(fieldsToValue.containsKey('Type') ,'Type is Not populated'); //It broke

Even if I querried Type it was not part of map keys as the value was null.

  • 1
    You're correct - getPopulatedFieldsAsMap() only has a map of populated fields (as the name suggests), and any field that's null will not show up in that map (even if it's included in the SOQL query). I just tested this myself as well – Brian Miller Jul 18 at 12:13
  • Ahh..if that the case how we can compare fieldname in query is match in the query result dynamically. I google around but only find whether we can only get all fieldname from describe object or specifically state field name(i am not expert enough) . I wonder any api that enable to extract field name from soql so we can compare with result? Because currently I plan to put all the fieldname is string then use dynamic apex. – unidha Jul 19 at 2:19

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.