2

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?

4
  • field exist in object or added in soql ?
    – Ratan Paul
    Commented Jul 26, 2016 at 10:52
  • Field exist in the SOQL query
    – Gres
    Commented Jul 26, 2016 at 10:54
  • I can to serialize and desalinize the record, but it is very expensive.
    – Gres
    Commented 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? Commented Jul 26, 2016 at 11:43

1 Answer 1

6

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

getPopulatedFieldsAsMap()

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_getPopulatedFieldsAsMap

See also:

https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_sobject_getmap.htm

2
  • 1
    I presume, getPopulatedFieldsAsMap can't be used for List<sObject> of records Commented 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; } } Commented 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 .