3

When I run the below code, it gives me this error:

Invalid field Account.PersonMobilePhone for Opportunity.

Code:

Opportunity opplist  = [Select id,account.PersonMobilePhone from Opportunity limit 1]l 
opplist.get('Account.PersonMobilePhone');
1

1 Answer 1

11

You can't just call get cross object. You have to cache each step of the way if you want to use the dynamic approach:

Opportunity record = [SELECT Account.Name FROM Opportunity LIMIT 1];
Account parent = (Account)record.getSObject('Account');

Object parentFieldValue;
if (parent != null)
    parentFieldValue = parent.get('Name');

You could easily write a utility for such functionality. I wrote one such and called it CrossObject in an example here.

Note that you can just use a static reference and it's much easier to work with. No need to check each relationship along the path for null, and no need for a utility or multiple lines.

String accountName = record.Account.Name;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .