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
Semira@gmail.comSemira@gmail.com 

looping thorugh sub query

Hi all, 

Okay so I'm pretty weak at using sub query and usually avoid this. So I will need a little help on this. 
 
List<Job__c> jobs = [SELECT ID, Contact__c, Project_site_contact_name__c, (SELECT ID, Contact__c FROM Job_Contacts__r) FROM Job__c WHERE ID in :updateContactIds];
	for (Job__c job : jobs)
	{		

		for(Contact con : jobs.job_contacts__r)
		{

                }
        {
So here's the little code snippet. I the 2nd for loop is giving me error saying "Initial term of field expression must be a concrete SObject: LIST&lt;Job__c&gt;"

If I understand correctly the error is saying Job.job_contact__c is not a list. How do I check through the sub query? 
Best Answer chosen by Semira@gmail.com
Virendra ChouhanVirendra Chouhan
Hi,
In your code you write jobs.job_contacts__r you created a new instance for jobs that is job so use job.job_contact__r. 
and this is a type of job_Contact__c sobject so in 2nd for loop you must have to use for(job_contact__c con : job_job_contacts__r)
so try this code
for(Job_Contact__c con : job.job_contacts__r) { }


 

All Answers

Roy LuoRoy Luo
I believe you have a typo in the the second loop
for(Contact con : jobs.job_contacts__r)

It should be job instead of jobs:
for(Contact con : job.job_contacts__r)
Virendra ChouhanVirendra Chouhan
Hi,
In your code you write jobs.job_contacts__r you created a new instance for jobs that is job so use job.job_contact__r. 
and this is a type of job_Contact__c sobject so in 2nd for loop you must have to use for(job_contact__c con : job_job_contacts__r)
so try this code
for(Job_Contact__c con : job.job_contacts__r) { }


 
This was selected as the best answer
Yoganand GadekarYoganand Gadekar
See if this article on relationship query can help you:
http://www.cloudforce4u.com/2013/06/sometimes-we-need-list-of-all-child.html