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
JeffroJeffro 

Looping through List Objects

I created a query(controller) that retrieves Contact records based on certain 'where' conditions. Once I retrieve those records, I need to use the id to retrieve information from child objects. I am getting confused on the concept of how to "nest" these queries.

So, if the results of the Contact query are 50 Contacts, I need to loop through each of those records to attain information from a couple of other child objects.

 

Psuedo code:

Select Id, FirstName, LastName from Contact where Status = 'Applicant';

Loop:

Select X, Y, Z from child_object where Contact_Id = Contact.Id (from first query)

 Select X, Y, Z from child_object2 where Contact_Id2 = Contact.Id (from first query)

End Loop;

 

Then I can take the information and display it on a Visualforce page. Any ideas? I am just having problems associating the Contact results to a variable so I can use it against other objects.

 

Thanks,

Jeff

BritishBoyinDCBritishBoyinDC

You shouldn't need to loop - SOQL lets you build nested parent-child queries i.e. Select Id, LastName, (Select Id, Subject from Tasks) from Contact

 

VisualForce then lets you access the results of the sub query and iterate over the results. The easiest example is in the 'cookbook' - it selects a single account and associated contacts, but the principle is the same: 

http://wiki.developerforce.com/index.php/Force_Platform_Cookbook

 

Look at the section about building a table of data. The main difficulty you'll have is working out the child relationship names - the SOQL explorer or the salesforce.schema utility in Ecplise can help you do that though. Also, parent-child queries have pretty strict governor limits on the number of child rows that can be returned for a parent, so test carefully...

 

You should also look in the API docs for a description of SOQL Parent-Child queries. 

JeffroJeffro

Thanks for your response. The one issue I may have with that is that I need to base the criteria on a field in a child object.

For example:

 

Select FirstName, LastName, (Select child_field1 from child_object where child_field = 'Incomplete')

From Contact

where child_field = 'Incomplete';

 

So I guess I am confused because I need to limit the results from the Contact object based on criteria from one of the child objects. How can I specify that criteria?

The objects that I am using are Contacts, Application__c, and Enrollments__c and I want to grab all Contacts where the Application__c.Status = 'Incomplete'. I thought that I would be able to query for the Contact_id on the Application__c object, then use that Id to get all other related information.

BritishBoyinDCBritishBoyinDC

You can do that as well - you need to read up on SOQL queries in the api reference (http://www.salesforce.com/us/developer/docs/api/index.htm)

 

For the query you want, you can do something like this - SOQL lets you reference the parent using this syntax:

 

 

Select contact.firstname, contact.lastname, child_field1 from child_object where child_field = 'Incomplete')

 

 The APEX explorer can also help you generate the correct SOQL (http://wiki.developerforce.com/index.php/Apex_Explorer)