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
S N 27S N 27 

soql retrieved without querying the requested field while accessing related fields in the for loop

Hi All,
I have an Object called Account_Contact__c that has a field in masterdetail to account named Accounts__c.I am trying to access the Account's owner id in the for loop.
for(Account_Contact__c acn : MyList){

I have a set here that checks for all the account owners.
MySet.contains(acn.Account__r.OwnerId);
}

Is this the correct approach. There is no error while saving the code. But on the UI iam getting the error,"soql retrieved without querying the requested field".

Any help would be highly appreciated.
@Karanraj@Karanraj
This the run time error. You have to include the fields in your SOQL if you are going to refer the field in the apex code
For example
Select Id,Name__c,Account__r.OwnerID from Account_Contact__c