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
harshasfdcharshasfdc 

Querying 2 sobjects

Hi All,

 

can we query 2 sobjects in a single soql query.please help me out

 

 

Thanks 

Harsha

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

There is two type of query in salesforce.

 

SOQL and SOSL Queries

SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries.

 

For example, you could retrieve a list of accounts that are named Acme:

 

List<Account> aa = [select id, name from account where name = 'Acme'];

 

SOSL

SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type. The result lists are always returned in the same order as they were specified in the SOSL query.SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger. If a SOSL query does not return any records for a specified sObject type, the search results include an empty list for that sObject.


For example, you can return a list of accounts, contacts, opportunities, and leads that begin with the phrase map:

 

List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead];

 

Account [] accounts = ((List<Account>)searchList[0]);

Contact [] contacts = ((List<Contact>)searchList[1]);

Opportunity [] opportunities = ((List<Opportunity>)searchList[2]);

Lead [] leads = ((List<Lead>)searchList[3]);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

There is two type of query in salesforce.

 

SOQL and SOSL Queries

SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries.

 

For example, you could retrieve a list of accounts that are named Acme:

 

List<Account> aa = [select id, name from account where name = 'Acme'];

 

SOSL

SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type. The result lists are always returned in the same order as they were specified in the SOSL query.SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger. If a SOSL query does not return any records for a specified sObject type, the search results include an empty list for that sObject.


For example, you can return a list of accounts, contacts, opportunities, and leads that begin with the phrase map:

 

List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead];

 

Account [] accounts = ((List<Account>)searchList[0]);

Contact [] contacts = ((List<Contact>)searchList[1]);

Opportunity [] opportunities = ((List<Opportunity>)searchList[2]);

Lead [] leads = ((List<Lead>)searchList[3]);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Jimmy YJimmy Y

Say, if you wanna search Account along with Contacts under them, you can use SOQL:

 

SELECT Name,

    (SELECT FirstName,  LastName FROM Contacts)

FROM Account

 

Then you can get the Name info of the Accounts, and all contacts under these accoutns as well. 

 

In the second statement, "Contacts" indicates that there is a "Relathionship" between Object Contact and Account, from the side of Account, the "Relathionship" to Contact is "Contacts", this Relathionship can be used in SOQL. 

 

You can get more info about "Relashionship Query" in salesforce online help or the Apex laguage development Reference

 

harshasfdcharshasfdc

Hi Jimmy,

 

Thanks for the solution it helped me out

 

Thanks,

Harsha