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
Andrew AldisAndrew Aldis 

Iterating through a query using list results.

I am writing a class that will query a list of subsidiary accounts and return the account id's then iterate over those query results to return a list of related contacts, but I keep getting errors.  The object I am querying from is a custom object called account plans and I am trying to query a child records of a junction object called subsidiary accounts, I then want to take those list results and and query contacts related to the subsidiary accounts and add them to  select options list for a VF page.  The error I get is 'Invalid bind expression type of Subsidiary_Accounts__c does not match domain of foreign key' and my code is below.

 system.debug('conList list = '+conList.Size());
        subs = [Select Account__c From Subsidiary_Accounts__c Where Account_Plan__c = :plan.Id];
        system.debug('Subs list = '+subs.Size());
        system.debug('subs id 1 ='+subs[0]);
        system.debug('subs account id 1 ='+subs[0].Account__c);
      subConTemp =[Select c.Name, c.Title, c.Id From Contact c Where  ContactInformation__c = true and Contact.AccountId in :subs Order by Name Asc]; 
        for(Contact temp :subConTemp){
            conList.add(new SelectOption(temp.Id+' '+temp.Title, temp.Name));
       }
Best Answer chosen by Andrew Aldis
Nayana KNayana K
Set<Id> setIdAcc = new Set<Id>();   
     for(Account_Plan__c objAP :[Select Account__c From Subsidiary_Accounts__c Where Account_Plan__c = :plan.Id])
{
setIdAcc.add(objAP.Account__c); 
subs.add(objAP);
}
        system.debug('Subs list = '+subs.Size());
        system.debug('subs id 1 ='+subs[0]);
        system.debug('subs account id 1 ='+subs[0].Account__c);
      subConTemp =[Select c.Name, c.Title, c.Id From Contact c Where  ContactInformation__c = true and Contact.AccountId in :setIdAcc Order by Name Asc]; 
        for(Contact temp :subConTemp){
            conList.add(new SelectOption(temp.Id+' '+temp.Title, temp.Name));
      }