I was very excited to learn about the new getPopulatedFieldsAsMap() method added to the SObject class with Summer '16. Unfortunately upon testing I found what appears to be a bug based on my understanding. The method is intended to allow developers to easily obtain fields populated in memory. According to the docs:

A field is populated in memory in the following cases.

  • The field has been queried by a SOQL statement.

  • The field has been explicitly set before the call to the getPopulatedFieldsAsMap() method.

In practice, I've found that if a field is not included in a SOQL query when retrieving an existing record it will be excluded from the field map returned by the new method, even if it was explicitly set before the call:

Contact c = [SELECT Name FROM Contact LIMIT 1];
c.Title = 'President';
Map<String,Object> fieldsMap = c.getPopulatedFieldsAsMap();
// DEBUG|{Id=003XXXXXXXXXXXXXXX, Name=Jack Rogers}
// Note: Title field is not returned

However, a field explicitly set before the call is included in the case of a new SObject instance:

Contact c = new Contact();
c.Title = 'President';
Map<String,Object> fieldsMap = c.getPopulatedFieldsAsMap();
// DEBUG|{Title=President}

Here is a gist that can be run in the dev console to see first hand what I'm experiencing. AFAICT this seems like a bug, but I'm curious to hear if others see it as expected behavior.

@AndyFawcett has kindly opened a case with Salesforce developer support-I will update this post with their response once we know more.


1 Answer 1


The apex dev team saw Andy's case and the resulting discussion and logged bug W-3223800 for this. The behavior you're seeing is a bug.

The fix is scheduled for Spring 17, although safeharbor as always. Also, I've been informed it will be a versioned change, so be sure to update the API version of your apex classes to v39.0 or later.

Maps of Populated SObject Fields Return Correct Values

  • I still got issue now with version API -46 .
    – unidha
    Jul 18, 2019 at 7:32

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.