+ Start a Discussion
j.vinodj.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
Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

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.

 

 

Message Edited by XactiumBen on 01-21-2009 02:58 PM

All Answers

china.leafchina.leaf
Hi, it works well:

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:
23:11:42 INFO  - 20090108151143.181:AnonymousBlock: line 3, column 30: SOQL query with 5 rows finished in 13 ms
line 5, column 1: SelectLoop:LIST:SObject
line 7, column 5: fobjContact:Contact:{AccountId=0014000000IQlG6AAL, Id=0034000000QFX6IAAX}
line 9, column 5: fobjAccount:Account:{Name=Edge Communications, Id=0014000000IQlG6AAL}
line 10, column 5: fobjAccount.Name:Edge Communications
line 7, column 5: fobjContact:Contact:{AccountId=0014000000IQlG6AAL, Id=0034000000QFX6JAAX}
line 9, column 5: fobjAccount:Account:{Name=Edge Communications, Id=0014000000IQlG6AAL}
line 10, column 5: fobjAccount.Name:Edge Communications
line 7, column 5: fobjContact:Contact:{AccountId=0014000000IQlG7AAL, Id=0034000000QFX6KAAX}
line 9, column 5: fobjAccount:Account:{Name=Burlington Textiles Corp of America, Id=0014000000IQlG7AAL}
line 10, column 5: fobjAccount.Name:Burlington Textiles Corp of America
line 7, column 5: fobjContact:Contact:{AccountId=0014000000IQlGAAA1, Id=0034000000QFX6NAAX}
line 9, column 5: fobjAccount:Account:{Name=Grand Hotels & Resorts Ltd, Id=0014000000IQlGAAA1}
line 10, column 5: fobjAccount.Name:Grand Hotels & Resorts Ltd
line 7, column 5: fobjContact:Contact:{AccountId=0014000000IQlGAAA1, Id=0034000000QFX6OAAX}
line 9, column 5: fobjAccount:Account:{Name=Grand Hotels & Resorts Ltd, Id=0014000000IQlGAAA1}
line 10, column 5: fobjAccount.Name:Grand Hotels & Resorts Ltd



Message Edited by china.leaf on 01-08-2009 07:13 AM

Message Edited by china.leaf on 01-08-2009 07:16 AM
XactiumBenXactiumBen

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.

 

 

Message Edited by XactiumBen on 01-21-2009 02:58 PM
This was selected as the best answer