function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve Berley [Left Propeller]Steve Berley [Left Propeller] 

identifying with apex the fields in a query that spans multiple objects

The following is example code but it gets the point across...
string query = 'select id, lastName, account.name, account.customField__c from contact';
list<contact> queryResults = Database.query(query);
How can I determine the fields in the query result using code?  


 
Best Answer chosen by Steve Berley [Left Propeller]
Steve Berley [Left Propeller]Steve Berley [Left Propeller]
The answer is:  New in Spring '16 is Map<String, Object> getPopulatedFieldsAsMap() which does exactly this.
See:  https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_sobject_getmap.htm 

Big thanks to @Sebastian Kessel on SSE.

All Answers

Steve Berley [Left Propeller]Steve Berley [Left Propeller]
My goal is to create a method that takes a generic query result and returns a csv file.  To do so, I need the ability to interrogate the field names
Steve Berley [Left Propeller]Steve Berley [Left Propeller]
The answer is:  New in Spring '16 is Map<String, Object> getPopulatedFieldsAsMap() which does exactly this.
See:  https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_sobject_getmap.htm 

Big thanks to @Sebastian Kessel on SSE.
This was selected as the best answer