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

Find out what fields were returned after executing a SOQL string

I have APEX code that executes a SOQL query dynamically and returns a list of sObject. The SOQL string is only known at runtime (stored in Custom Settings). So I don't know which fields were actually queried.

Now if I want to iterate through the result list and dispaly kind of a table of all values, I would need to know which fields were queried in the SOQL string.


Is there a way to find out what fields are contained in the single sObjects?




You can see the fields queried when you are firing the SOQL query .  .. Can you give an example how you are querying ?


Do you mean parse the query string and find out which field were queried? Well parsing a SOQL string can be extremely complicated since you might have to deal with agregate functions, relationship fields or sub-queries.


SELECT Id, Name, Owner.Name, (SELECT Name, Stage FROM Opportunities), Industry FROM Account WHERE Id IN (SELECT Account FROM Contact WHERE Email LIKE '') ORDER BY Industry


Try to parse this for which fields were queried :)


But again, the SOQL query can be anything and it is configured by an Admin in the Custom Settings.


I am querying like this


List<sObject> L = Database.query(mySoqlString);


where mySoqlString is taken from the Custom Settings


Sorry .. probably I got your question wrong :


I thought you just want to see the query not the query fields..


System.Debug(mySoqlString );


will show the query....