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
Salvatore GomezSalvatore Gomez 

Return data fields from Multiple sobject in single query to visualforce page

I always seem to struggle with returning data fields from Multiple sobject in single query to visualforce page. Every time i try to display one of the opportunity fields in a visualforce page with a query similar to below i always get a invalid field for sobject error which I get because the list type is an account. I'm just trying to understand what is the best method for retrieving the result set from the below query and displaying it in visualforce. any help is greatly appreciated, thanks in advanced.

List<Account> Acc = new List<Account>();

Acc = [SELECT Id,Name,PersonEmail
 ,(Select Attachment.Id, Attachment.ParentId from Account.Attachments where Name='Contact Picture',(SELECT Opportunity.PLAN_DESCR__c,Opportunity.ADMIT_TERM_DESCR__c,Opportunity.JOB_TITLE__c,Opportunity.AccountId FROM Account.Opportunities)
            FROM Account Where Id in
                (SELECT AccountId FROM Opportunity where PLAN_DESCR__c='Business Analytics' and ADMIT_TERM_DESCR__c='2016 Fall')
            limit 20                 
     ];
Ankit SehgalAnkit Sehgal
The parenthesis in your Attachment sub-query is not matched up or closed .
Try this query
[SELECT Id,Name,PersonEmail
 ,(Select Attachment.Id, Attachment.ParentId from Account.Attachments where Name='Contact Picture'),(SELECT Opportunity.PLAN_DESCR__c,Opportunity.ADMIT_TERM_DESCR__c,Opportunity.JOB_TITLE__c,Opportunity.AccountId FROM Account.Opportunities)
            FROM Account Where Id in
                (SELECT AccountId FROM Opportunity where PLAN_DESCR__c='Business Analytics' and ADMIT_TERM_DESCR__c='2016 Fall')
            limit 20                 
     ];

 
Salvatore GomezSalvatore Gomez
Thanks for catching that Ankit, i think when i was copying and pasting it to the forum i must have deleted it by mistake since the parenthesis was present in the apex class when i went to check it out.
Ankit SehgalAnkit Sehgal
The query looks fine. I tried it in my org by removing the custom fields and it worked fine.
Did you tried it in workbench ?
Salvatore GomezSalvatore Gomez
Hi Ankit,

Thanks, yes the query worked fine in both workbench and the developer console. how would I return the result set of this query to display in a visualforce data table?