function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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')