You need to sign in to do that
Don't have an account?
MrTheTyler
Determine the fields queried on an sobject
More of a theoretical question - in the displayfields function below is it possible to do what I've done in pseudocode?
main(){ sobject acc = [select name, phone from account limit 1]; displayfields(acc); } static void displayfields(sobject x){ //somehow loop through the available fields on the sobject //and display them for(field f:x.fields) system.debug(f.fieldname); //should print out "name" , "phone" }
Thanks!
Tyler
let me know if this code helps you!
While very nice code for showing all fields defined on an sobject, I'm not sure it shows me what fields have been retrieved in a query. For example:
I could see using your code to get all the field names and then use a try catch block to brute force a solution. Any idears on a more graceful solution?
Merry Christmas!!
Tyler
If you're defining a static SOQL query - why do you need to know the fields? I would think that you could use Dynamic SOQL and reference the string of fields you create in the query string?
The question is, at this time, theoretical and aimed at getting a deeper understanding of sobject data structures. However, I can imagine a utility routine that can be called from many different places, passing in different sobject types with varying subsets of their fields available.
Using @[Salesforce Expert]'s schema exploring code, I could see looping through every field available for a sobject type on a sobject instance and catching errors for accessing those unqueried fields - however, my gut tells me there should be some means for directly getting a set of field names queried and therefore available for a sobject instance.
Thanks for your time,
Tyler
If we were in MS SQL land or another RDBMS, there are ways to find out that information rather easily. However in SF - as far as my experience has shown me, you can ask the system questions about objects (getDescribe, etc) but you cannot ask it the specific question you are seeking.
If you find out otherwise, please comment back on this post - I'd be interested to hear how!
We’ve added a method on the Apex SObject class that makes it more efficient to iterate over fields that have been populated in memory.
Previously, iterating over just the populated the fields of an SObject involved some complicated programming. For example, if you queried an SObject using SOQL, it wasn’t easy to tell which fields were returned. In Summer ’16, we’ve introduced a new method on the SObject class that returns a map of populated field names and their corresponding values: Map<String, Object> getPopulatedFieldsAsMap()
http://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_sobject_getmap.htm
Actually, looks like there's a method for this now: `getPopulatedFieldsAsMap`
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_getPopulatedFieldsAsMap