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
    Jul 26 '16 at 10:52
  • Field exist in the SOQL query
    – Gres
    Jul 26 '16 at 10:54
  • I can to serialize and desalinize the record, but it is very expensive.
    – Gres
    Jul 26 '16 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 '16 at 11:43
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
  • I presume, getPopulatedFieldsAsMap can't be used for List<sObject> of records Jun 26 '17 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; } }
    – Shane K
    Dec 17 '20 at 15:34

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.