Skip to content

Instantly share code, notes, and snippets.

@scottbcovert
Last active June 29, 2016 15:12
Show Gist options
  • Save scottbcovert/6b2ce8c5de85e8f0735c2c5469ba3bf2 to your computer and use it in GitHub Desktop.
Save scottbcovert/6b2ce8c5de85e8f0735c2c5469ba3bf2 to your computer and use it in GitHub Desktop.
Troubleshooting getPopulatedFieldsAsMap() Apex Method
Contact c = [SELECT Name FROM Contact LIMIT 1];
c.Title = 'President';
Map<String,Object> fieldsMap = c.getPopulatedFieldsAsMap();
System.debug(fieldsMap);
// DEBUG|{Id=003XXXXXXXXXXXXXXX, Name=Jack Rogers}
System.debug('Field Map Size: ' + fieldsMap.size());
// DEBUG|Field Map Size: 2
for (String fieldName : fieldsMap.keySet())
{
System.debug('Field Name: ' + fieldName);
}
// DEBUG|Field Name: Id
// DEBUG|Field Name: Name
Object TitleValue = fieldsMap.get('Title');
System.debug('Title Value: ' + TitleValue);
// DEBUG|Title Value: null
Boolean containsTitle = fieldsMap.containsKey('Title');
System.debug('Does FieldsMap Contain Title? - ' + String.valueOf(containsTitle));
// DEBUG|Does FieldsMap Contain Title? - false
c = new Contact();
c.Title = 'President';
fieldsMap = c.getPopulatedFieldsAsMap();
System.debug(fieldsMap);
// DEBUG|{Title=President}
System.debug('Field Map Size: ' + fieldsMap.size());
// DEBUG|Field Map Size: 1
for (String fieldName : fieldsMap.keySet())
{
System.debug('Field Name: ' + fieldName);
}
// DEBUG|Field Name: Title
TitleValue = fieldsMap.get('Title');
System.debug('Title Value: ' + TitleValue);
// DEBUG|Title Value: President
containsTitle = fieldsMap.containsKey('Title');
System.debug('Does FieldsMap Contain Title? - ' + String.valueOf(containsTitle));
// DEBUG|Does FieldsMap Contain Title? - true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment