I'm querying via the REST API (in Apex, because I'm calling another org, amongst other reasons) and using a wacky recursive algorithm on the first returned record to parse out the field names. And no, for my purposes, I cannot parse the fields names from the query string, for a multitude of reasons.
Sample query
SELECT Name, (SELECT Firstname, Account.Id From Contacts) FROM Account LIMIT 2
My result when all is well. This is what I'm looking for.
"fieldToObjectWrapper": {
"null": {
"Name": "Account"
},
"Contacts": {
"Contact.Account.Id": "Account",
"Contacts.FirstName": "Contact"
}
}
My problem comes when the first returned record has no child records. The child node doesn't exist, so I can't parse the fields.
QueryResult with a "good" first record
{
"attributes": {
"url": "/services/data/v44.0/sobjects/Account/0016100000W5K09AAF",
"type": "Account"
},
"Name": "Company A",
"Contacts": {
"totalSize": 1,
"done": true,
"records": [
{
"FirstName": "Whoever",
"attributes": {
"url": "/services/data/v44.0/sobjects/Contact/0036100001aVoyOAAS",
"type": "Contact"
},
"Account": {
"attributes": {
"url": "/services/data/v44.0/sobjects/Account/0016100000W5K09AAF",
"type": "Account"
},
"Id": "0016100000W5K09AAF"
}
}
]
}
},
{
"attributes": {
"url": "/services/data/v44.0/sobjects/Account/0016100000W5K0AAAV",
"type": "Account"
},
"Name": "Company B"
}
But imagine if Company B came first. No Contacts so no fields to parse.
It dawned on me that the Workbench must be doing something similar to display its query results so I dug into the open source code. My PHP is... lacking... but from what I can tell, they're actually doing the same thing (which made me feel good about myself). They're taking the first record from the results and parsing out the names.
https://github.com/forceworkbench/forceworkbench/blob/master/workbench/async/QueryFutureTask.php
On line 237, in createQueryResultTable, they're passing $records[0] into getQueryResultHeaders, which then calls itself recursively as it loops through the fields.
But in the Workbench, it works. (Maybe that's why they call it the Workbench...) They get the field names regardless of whether the first record contains child records or not.
How do they do that? What am I missing? Is there another way?
Thanks very much!
UPDATE Question answered in the comments. Thanks to all for playing!
Firstname, Account.Id
). Can't we create table header using these fields?