3

This question seems silly but i am really stuck! I want to get the Account name and Account Rating for my contacts via Apex code, but instead of Account name and rating, it returns the accountId! I run the code in anonymous Console. Any ideas of what i am doing wrong?

List<sObject> contacts = [select id, name, AccountId, Account.Name, Account.Rating from Contact];
system.debug(contacts); 

But in the list returned as you can see, Account Name and Rating are not displayed. enter image description here

2
  • Check FLS on those fields Commented May 11, 2020 at 10:53
  • Hi Rahul, My user has access in those fields. Commented May 11, 2020 at 10:59

1 Answer 1

9

This is just how the Developer console shows the details. It does not show any parent details but you are actually getting those details.

List<Contact> contacts = [select id, name, AccountId, Account.Name, Account.Rating from Contact];
system.debug(contacts); 

for (Contact individualContact: contacts){
    System.debug(individualContact.Account.Name); // Should get the value here
}
1
  • Thanks manjit5190!!! Commented May 11, 2020 at 11:29

You must log in to answer this question.

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