+ Start a Discussion
SFDC coderSFDC coder 

How to retrieve data from multiple objects in one query?

Hi all,

I have six queries and i would want to retrieve the data by merging those queries into a single one . How can this be done?

retrieveUserProfile  -> SELECT Mobile_Configuration_Profile__r.Name, Mobile_Configuration_Profile__r.API_Access__c FROM UserToMobileLink__c WHERE User__c = 'userId’
retrieveCurrentUserDetails -> select  Languages__c, User_Sales_Org__c from User where Id = ‘userId’
retrieveLocaleObject -> Select Name, Translated_Text__c from Translations__c where Language__c = 'userLocale'
retrieveUserProfileSpecification -> select Id, Name, Tab__c ,Create__c, Delete__c,Update__c, SOQL_Data_Filter__c from Mobile_Configuration_Item__c where Mobile_Configuration_Profile__r.Name = 'profileName’ ORDER BY Id.
Ajay K DubediAjay K Dubedi
Yes we can retrieve data from multiple objects in a single query using SOSl.
 for eg.
List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName)];
this query searches for accounts and contacts that have any fields with the word 'SFDC'.
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11

If multiple sobject records get into one query is possible to do must and should have relationship of multiple sobject

Eswar Prasad.
SFDC coderSFDC coder
Thanks Ajay..

But if you look at my queries they have nothing in common that can be specified in the the Find clause. Then how will it work?
sandeep sankhlasandeep sankhla

If those queries are on different objects then you need to do seprate query..only in one case where they are related with each oither then you can do in on equery as subquery....

You can use relationship query if they have relation

Like Account is parent and conatct, opportunity lead is child let say

then you can use on equery and many child sub query like below

select id (select id from conactts) (select id from opportunites) from account...

but if there is no relation then seperate query only we need to do , we cant combined them..

SFDC coderSFDC coder
thanks sandeep,

This means that in order to combine the queries i mentioned above there must be some relationship with atleast one object right? i.e one object A should be parent of all those tables from which am quering Right?

sandeep sankhlasandeep sankhla

No but yes as other said you can make us eof SOSL instead os SOQL to make this ...but there also restiction as they mentioned above filter and all.....can you tell me your requirnments so we can guid eyou for correct path.whether you should use SOQL or SOSL...

Ajay K DubediAjay K Dubedi
Use SOSL when
*You don't know in which object or field the data resides and you want to find it in the most efficient way possible.
*You want to retrieve multiple objects and fields efficiently, and the objects may or may not be related to one another.
*You want to retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.
SFDC coderSFDC coder
hi ajay,

Am trying to achievet the second point from what you have mentioned. But in that case what do i mention in the find clause ? since the tables are not related to each other
Hi SFDC  Coder  ,

I will suggest you one may be it will help !

Try install this app from appexchange ,it has only 7 -8 Scontrol means webpage which will help you to built query from multiple object on click .

Once you will install this you will get AjaxExplorer on etab in this app . Onclick of that tab it will open on editor like DataLoader,workbench .However it will allow you to built complex to complex query on click of check box .

Please let me know if it helps !!

Ganesh RajputGanesh Rajput
List<List<SObject>> searchList = [FIND {SFDC} IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName)];