You need to sign in to do that
Don't have an account?

Grab related objects from SOQL?
I'm trying to grab all the notes for every contact associated with an account. I built a controller to do it:
public class aggregateNotes{ private Opportunity opp; public aggregateNotes(ApexPages.StandardController controller) { this.opp = (Opportunity)controller.getRecord(); } // container for the notes (not used, yet) public class row { public string LastName {get; set;} public string Body {get; set;} public row(sObject o){ LastName = (String)o.get('LastName'); Body = (String)o.get('Body'); } } public List<row> getRelatedNotes(){ // get the account id from the opportunity opp = [SELECT id, Account.id FROM Opportunity WHERE id = :opp.id]; List<row> rows = new List<row>(); for(sObject o : [SELECT LastName, (SELECT Body FROM Notes) FROM Contact WHERE Account.Id = :opp.Account.id]){ // this fails! WTF? System.debug(o.get('Body')); } return rows; } }
I get a "System.SObjectException: Invalid field Body for Contact" on the System.debug(o.get('Body')) call. How do I access the body field in that SOQL?
You try to get access to the field 'Body' of Contact, but it's Note's field. Modify your code like this:
All Answers
Hi!
First, I don't think there's a Notes standard object in SFDC. You should be using Note.
Secondly, I see you're going for maximum efficiency in your code, trying to get as much done in a few lines. Great! but when it comes to debugging it complicates things. Try to split your code into smaller steps, like having 2 sepparate soqls etc.
Third, you're getting that error cause the SELECT Body FROM Notes part retrieves a list of records, not individual fields. You would need to itterate through that list and show the Body field. There's a level that's missing from the relationship, to put it in a simple way, O.Body should be smth like O.<Note>.Body...
My solution would be like this:
Once you have the list of related notes you can create the container(wrapper) list and use it in your page.
Hope this helps :),
Adrian
You try to get access to the field 'Body' of Contact, but it's Note's field. Modify your code like this: