+ Start a Discussion
sornasorna 

Get cross object fields from a sobject

Hi,

I have to get a cross object field value from sobject. For ex: 

 

Contact c [Select Account.Name from Contact Limit 1];
Sobject s = (Sobject) c;
System.debug('Account Name: '+c.Account.Name);
System.debug('Account Name: '+s.get('Account.Name'));

 In the above code, the 1st debug will work where as for the 2nd debug, it is throwing an error that Account.Name is not a valid for Contact. Any idea on how to get the parent's field values from a SObject?

 

 

Thanks in advance,

Sorna

Best Answer chosen by Admin (Salesforce Developers) 
sornasorna

found out... we have to use getSObject(). like: s.getSObject('Account').get('Name')