+ Start a Discussion
Swiderski, ErikSwiderski, Erik 

How to combine unrelated objects in a query. Is that even a thing?

I'm fairly new to lightning component development. I'm trying to combine the query results for two unrelated objects into one "object" so I can show it in something slick like a treegrid, or a nested aura iteration.

I've done the "easy" ones before, using apex to query contacts and get parent fields (in this case the account)
[SELECT Id, Name, Phone, Email, Account.Name FROM Contact]
As well as the other way, to query account and get fields from contact
[SELECT Id, Name, (SELECT Email, Phone FROM Contacts) FROM Account]

But this project is a little unique. One of the objects (or "parent" if this was an actual relationship) is tasks. I have my query to get the task records, that part works. The custom object that I want to show in a treegrid or aura iteration is not related to the task. This is the tricky part. It could be considered a "child" because this custom object has a regular text field populated with the task's record id.

Is there a way to easily query tasks for a certain criteria filter, and include/group records of this custom object under each task that have the task's id in the text field? I'm not familiar with a "wrapper class" but it seems that combines maps and objects or something and might do what I need it to. Any suggestions would be appreciated.
Raj VakatiRaj Vakati
Well SOQL will not be an option for you if you wanted to retune the unrelated objects ,, i can suggest to use SOSL for unrelated objects result 
Swiderski, ErikSwiderski, Erik
Thanks for the quick reply, Raj. I'm not sure I understand, though. What exactly do you mean by "retune?" Not sure if it helps, but I'm not wanting to update any of the data during the transaction. When I said that I'm trying to combine the query results for two unrelated objects into one "object," I could've been clearer.

The "object" I mean is how the javascript in the lightning component controller pulls the query result from the apex controller class. In the second piece of code, for example: 
SELECT Id, Name, (SELECT Email, Phone FROM Contacts) FROM Account

console.log would show [object Object] in the browser dev console when I console.log the response.getReturnValue() from the query with only one result. If there were three results, [object Object],[object Object],[object Object]. These "objects" would work perfectly for the treegrid component (https://developer.salesforce.com/docs/component-library/bundle/lightning:treeGrid/example). It would show each account, and related contact below it- expandable with the chevron.

Is there a way to do this with two objects that are not related? In my example, the treegrid would use tasks instead of accounts, and instead of contacts, the custom object records that have the task's record id in a text field. To visualize: if it had a relationship, the query would look something like:
SELECT Id, Subject, (SELECT taskidfield__c, another_field__c FROM Custom_object_relationship__r) FROM Task