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
Ravi Dendhi 10Ravi Dendhi 10 

SOQL query to fetch multiple objects and display records in VF pages

Hi I am trying to fetch multiple objects and their records with the Task to display on VF pages. I know I can achive this using SOSL but we provide managed packages for the users so I cannot go with SOSL as it needs maintanence. I tried using SOQL but can only query one object at a time the query is Dynamic SOQL. Instead can I write the Dynamic SOQL query in For loop to achive this. Suggestions please. thanks 
Thota Rakesh 1Thota Rakesh 1
Hi Ravi Dendhi 10,

If you want to combine two different objects in a single table and display in a visual force page then use Wrapper class to combine two objects and display in your visual force page. Check this sample post to get start 

https://www.interactiveties.com/blog/2012/visualforce-wrapper-class.php

http://bobbuzzard.blogspot.co.uk/2010/09/rotating-visualforce-table.html

https://developer.salesforce.com/page/Wrapper_Class


Kindly mark it as solved if it helps you,


Best Regards,
Rakesh.T
SrikanthKuruvaSrikanthKuruva
You cannot query 2 different objects in a single SOQL call. And you should not be writing SOQL inside the forloop.
1.) Try relationship queries.
2.) it relationship queries dont help, you should be doing SOQL1 and loop over these results to collect any values that are required for SOQL2. for example
list<Id> listIds = new List<Id>();
for(obj1__c obj : [select id, field1 from obj1__c]){ //soql1
listIds.add(obj.field1); //collect the necessary data
}
for(obj2__c obj : [select id, field2 from obj2 where id in :listIds]){//soql2
//additional logic
​}