You need to sign in to do that
Don't have an account?
j.vinod
Retrieving Related Parent Fields using Dynamic Apex
Hi,
I have 2 Tables.
Oppurtunity object is the Parent of Job object.
Iam Querying something like this
String sSql ='SELECT Opportunity__r.Channel__c FROM Job__c where Id=\'a008000000BxwsS\'';
List <SObject> s = Database.query(sSql);
for (Sobject s1 : s){
Object O1 = s1.get('Opportunity__r.Channel__c');
Channel = (String)O1;
}
But It gives me error message
System.SObjectException: Invalid field Opportunity__r.Channel__c for Job__c
I know the field exists and I am able to see it in relationship. But I am unable to retrieve the field using Dynamic Apex "get" method.
Any Help is appreciated.
Thanks
Vinod
I have 2 Tables.
Oppurtunity object is the Parent of Job object.
Iam Querying something like this
String sSql ='SELECT Opportunity__r.Channel__c FROM Job__c where Id=\'a008000000BxwsS\'';
List <SObject> s = Database.query(sSql);
for (Sobject s1 : s){
Object O1 = s1.get('Opportunity__r.Channel__c');
Channel = (String)O1;
}
But It gives me error message
System.SObjectException: Invalid field Opportunity__r.Channel__c for Job__c
I know the field exists and I am able to see it in relationship. But I am unable to retrieve the field using Dynamic Apex "get" method.
Any Help is appreciated.
Thanks
Vinod
This is probably a bit late but if you want to get your data out you have to get the parent object first using getSObject().
E.g.
Channel = (String)s1.getSObject('Opportunity__r').get('Channel__c');
If you want to get out children from an SObject you can use s1.getSObjects('child relationship name');
Hope this helps.
All Answers
String fchrSoqlStr = 'Select c.Account.Name, c.AccountId From Contact c limit 5';
List <SObject> flstSobject = Database.query(fchrSoqlStr);
for(SObject s : flstSobject){
Contact fobjContact = (Contact)s;
System.debug('fobjContact:' + fobjContact);
Account fobjAccount = fobjContact.Account;
System.debug('fobjAccount:' + fobjAccount);
System.debug('fobjAccount.Name:' + fobjAccount.Name);
}
this is the debug message:
Message Edited by china.leaf on 01-08-2009 07:13 AM
Message Edited by china.leaf on 01-08-2009 07:16 AM
This is probably a bit late but if you want to get your data out you have to get the parent object first using getSObject().
E.g.
Channel = (String)s1.getSObject('Opportunity__r').get('Channel__c');
If you want to get out children from an SObject you can use s1.getSObjects('child relationship name');
Hope this helps.