I want to set the Description field on Contact object same as the Industry field on the Account object. But when I am executing the below code I am getting error A non foreign key field cannot be referenced in a path expression: AccountId. Can someone please explain why I am getting this error and how to go to the Industry field on the Account object from Contact object.

for(Contact c : [Select Id,AccountID,Description from Contact])
         c.description = c.AccountID.Industry;

You need to use the parent relationship name when you pull fields from upstream in a field path. For AccountId, the proper relationship name is Account.





Please note this field path must also be included in the query itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.