This question already has an answer here:
I am working on a module where I have to create complex nested JSON dynamically based upon configs stored in custom metadata, The metadata contains Object name, fields names as text fields.
Now the problem I am facing is I can't use Sobject get on relationship fields, it gives runtime error.
Minimum viable code to reproduce:
Opportunity opp = [Select id,Account.Name from Opportunity Limit 1]; opp.get('Account.Name');
Error on line 2, column 1: System.SObjectException: Invalid field Account.Name for Opportunity AnonymousBlock: line 2, column 1
How can I dynamically access relationship fields using Sobject get?