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
NipuWNipuW 

Please clarify the following

In SOQL when you do a select on Account and contact it returns a single list with accounts and contacts both. But with SOSL it rerutns nested list. Why?

List<Account> acctsWithContacts = [SELECT Name, (SELECT FirstName,LastName FROM Contacts)
        FROM Account WHERE Name = 'SFDC Computing'];

List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS 
           RETURNING Account(Name), Contact(FirstName,LastName)];


 
{tushar-sharma}{tushar-sharma}
Hi Nipul,

Because in SOQL It will only return those contact which are child of Accounts, So they came in Single list.
While in SOSL there can be totally different Account, Contact Which may be not related with each other. So thats why it return them as List of list.

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
NipuWNipuW
@Tushar, How it returns Accounts + related contacts in a single list? im not clear. Isnt is also List of accounts and a single account object can have list of related contacts? 
{tushar-sharma}{tushar-sharma}
It comes as nested record. Eg: if you want to access child record then you need to do Account.Contacts. In your code
List<Account> acctsWithContacts = [SELECT Name, (SELECT FirstName,LastName FROM Contacts)
        FROM Account WHERE Name = 'SFDC Computing'];
acctsWithContacts[0].Contacts; //It will return all child contacts