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
Praveen Kumar BonaluPraveen Kumar Bonalu 

how to query more than 2 objects in a class

How to query more than two objects in a class ?

Hi i am new to salesforce development.I have 2 standard objects say (Accounts and contacts) and 3 custom objects that are related to each other(Obj1,obj2,obj3).How do i write a query to fetch all the fields from  these 5 objects and display in my visual force page(i have to render as pdf with all these fields)
Shashikant SharmaShashikant Sharma
You can query as many object in a class as you want. Limit is on Number of SOQL statements. 

Like : 

Account acc = [ Select name from Account where Id = '001XXXXXXXXXXXX';
List<Contact> listOfContact = [  Select FirstName, LastName from Contact Where AccountiD =: acc.Id ];
List<Obj1> => Its query
List<Obj2> => Its query
List<Obj3> => Its query

As you mentioned objects have relationships you could query them in a single SOQL like account and contact are related 

Account acc = [ Select name,
                              ( Select FirstName, LastName From Contacts ) // here Contacts is relationship name
                           from Account where Id = '001XXXXXXXXXXXX';


Please mark this as answer if it helps you so it help others having same problem.


 
Praveen Kumar BonaluPraveen Kumar Bonalu
Hi shashikanth ..thanks for your reply......can you pls explain brifely the class and the visual force page to retrive the fields from the class ..........Thanks in advance
Cloud_forceCloud_force
If you want ot fetch records from different independent objects then write seperate Queries. If objects are related then you can use relationship Query.
You cannot Query all fields with a start (*) you have to specifically mention field API names.
http://www.forcexplore.com/2014/07/relationship-query-in-salesforce.html (http://www.forcexplore.com/2014/08/visualforce-and-apex-tutorial-for-beginners.html)

http://www.forcexplore.com/2014/08/visualforce-and-apex-tutorial-for-beginners.html