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

Referenced Fields in SOQL Queries
I am trying to write a fairly straightforward SOQL query, which is not working how i'd expect.
// Substituting valid where criteria for etc...
sObject Temp = Database.query('SELECT Contact.Account.Name FROM Contacts WHERE etc.');
System.Debug(Temp.get('contact.account.name'));
I get an 'Invalid field 'contact.account.name'' error whenever i try this.
I've tried these variations and all of their various capitalizations (not that it should matter)
Temp.get('name')
Temp.get('account.name')
Obviously, this works:
Contact Temp = [SELECT Contact.Account.Name FROM Contacts WHERE etc.'];
System.Debug(Temp.Account.Name);
But I need to be able to build the query string dynamically, querying against arbitrary salesforce objects. I feel like i'm just missing something simple, but I've been over the documentation numerous times with no luck.
Any assistance anyone could offer would be greatly appreciated.
Thanks,
Mark
Hi Mark,
If you want to build dynamic Queries you gotta do it this way:
If you want to retrieve child records from a parent you need to use getSObject
The getSObject method is for retrieving fields from a parent.
Also found this :
Setting and Retrieving Foreign Keys
To set or retrieve the record associated with a foreign key, use the getSObject and putSObject
methods. Note that these methods must be used with the sObject data type, not Object. For example:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_dml.htm
Hope this helps, it seems its done the same way either way around.
Gaston.
Hi,
Not sure your query is correct - do you have an extra 's' on contact?
Perhaps
will work ok??
R.