I have a contact in a generic sObject in apex and while I'm able to easily get detail field values using
sObject.get('MyContactField__c')
I'm not able to get fields off the related parent object, and I'm sure the related field is in memory, based on the debug logs. This returns a runtime error:
sObject.get('Account.MyAccountField__c')
with the error
Invalid field Account.MyAccountField__c' for Contact
How do I get a field value off of a parent on a generic sObject?
(String)obj.getSObject('Account').get('Name')sometime does not have value and apex is throwing an error in that case to counter this I have appliedobj.getSObject('Account') !=nullcheck but seems not working at all – Hunt Jan 23 '20 at 05:07sObject o = [SELECT Manager.Name FROM User LIMIT 1]; sObject manager = o.getSobject('Manager'); String name = manager != null ? (String) manager.get('Name') : 'Nope'; System.debug(name);. You could alsoSELECT ManagerIdand check that... for related lists - I think list will always be there (never null value) but it's perfectly OK for it to beisEmpty(). – eyescream Jan 23 '20 at 06:26