When you try to attempt to the field that not present in SOQL query, you get error like that:

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Account AnonymousBlock: line 5, column 1

How I can to check that the field field is present in SOQL query?

  • field exist in object or added in soql ?
    – Ratan Paul
    Jul 26, 2016 at 10:52
  • Field exist in the SOQL query
    – Gres
    Jul 26, 2016 at 10:54
  • I can to serialize and desalinize the record, but it is very expensive.
    – Gres
    Jul 26, 2016 at 10:56
  • Why do you need to check? Can't you just add all fields that you need to the SOQL query? Or is it dynamic in some way so that you can't predict which fields you'll need? Jul 26, 2016 at 11:43

1 Answer 1


There is a new method on sObject(as of Summer 16) for this:



See also:


  • I presume, getPopulatedFieldsAsMap can't be used for List<sObject> of records Jun 26, 2017 at 7:25
  • Great! I have one query for the grid, and one for the detail which includes an additional full-text field. Sample code that worked for me: public LogDTO(ErrorLog__c log) { Map<String, Object> m = log.getPopulatedFieldsAsMap(); this.id = log.Id; this.createdDate = log.CreatedDate; this.type = log.Type__c; this.status = log.StatusCode__c; this.relatedId = log.RelatedID__c; this.message = log.Message__c; if (m.keySet().contains('LogDescription__c')) { this.detail = log.LogDescription__c; } }
    – Shanerk
    Dec 17, 2020 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .