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
AshAsh 

Accessing Relationship fields dynamically

Hi,

 

I am usng dynamic soql, where I prepare my sqol query at runtime and would like to access the field values using dynamic field names used in my query. I am able to do it using sObject method  get(stringName) but this works only for non-relationship fields and not relationsip fields. Please let me know if I am missing anything and how to access relationship field using get() method.

 

Below is the simple code snippet

 

String qryString = "SELECT Id, AccountId,   Account.AccountNumber from Order limit 2"       (note the field names will be computed at runtime, I am just hard coding here)

 

Order[] ords=Database.query(qryString);

 

 

for(Order ord : ords){

System.debug('Account ID...'+ord.get('AccountId'));//This works and prints the accountID

System.debug('Account number..'+ord.get( 'Account.AccountNumber'); //get an error "Account.AccountNumber does not belong to SObject type Order

}

 

Thanks.

bob_buzzardbob_buzzard
Have you tried extracting the Account sObject from the order?  If that works, you may then be able to extract fields from the Account sObject.