3

I am trying to get the Ultimate Parent Account from my contact up to 3rd level parent. Here is my SOQL code:

SELECT Name,Account.Parent.Name, Account.Parent.Parent.Name, Account.Parent.Parent.Parent.Name FROM Contact

I expect to see this result:

Contact:{Name='Name of COntact',Parent='the parent name', ChildParent='Child Parent Name', GrandParent='Grand Parent Name'} 

OR

Contact:{Name='Name of COntact',ParentId='the parent Id', ChildParentId='Child Parent Id', GrandParentId='Grand Parent Id'}

Once I debug my query the result is not what i expected.

Contact:{Name='Name of the Contact', AccountId='Parent Id', Id='Id of the Contact'}

Anyone help?

4

System.debug() doesn't recursively output the details of related sObjects, like Contact.Account, that you may have queried. It only includes fields that are directly located on the record you're outputting to the debug log.

If you wish to debug information about related records, refer to them directly:

Contact c = [SELECT Id, Account.Name, Account.Parent.Name FROM Contact];

System.debug(c);

11:52:07:009 USER_DEBUG [3]|DEBUG|Contact:{Id=0031K00002bgN4BQAU, AccountId=0011K000023PAOCQA4}

System.debug(c.Account);

11:53:17:011 USER_DEBUG [4]|DEBUG|Account:{Id=0011K000023PAOCQA4, Name=TestAccount, ParentId=0011K0000253jWdQAI}

System.debug(c.Account.Parent);

11:53:17:011 USER_DEBUG [5]|DEBUG|Account:{Id=0011K0000253jWdQAI, Name=Parent}

| improve this answer | |
  • I tried to use the System.debug(c.Account); I got an error Variable does not exist: Account. I am very sorry I am a newbie to SOQL – Leorah Sumarong Feb 4 '19 at 17:00
  • Leorah, this probably is because either your variable is a List<Contact> or is not called c. Would you please edit your question to show the complete code you are using? – David Reed Feb 4 '19 at 17:08
  • I got it now @David, the problem is i put in my parameter as List<sObject> varName, So I change it to List<Contact> varName to get the Variable. Thank you very much, you did a great help. – Leorah Sumarong Feb 4 '19 at 17:14
7

Another way would be to use JSON.serializePretty

Contact c = [SELECT Id, Account.Name, Account.Parent.Name FROM Contact];

System.debug(JSON.serializePretty(c));

JSON:

 {
  "attributes" : {
    "type" : "Contact",
    "url" : "/services/data/v44.0/sobjects/Contact/0030C000001epfBQAQ"
  },
  "Id" : "0030C000001epfBQAQ",
  "AccountId" : "0010C000002L2l4QAC",
  "Account" : {
    "attributes" : {
      "type" : "Account",
      "url" : "/services/data/v44.0/sobjects/Account/0010C000002L2l4QAC"
    },
    "Id" : "0010C000002L2l4QAC",
    "Name" : "Child Account",
    "ParentId" : "0010C000002L2kzQAC",
    "Parent" : {
      "attributes" : {
        "type" : "Account",
        "url" : "/services/data/v44.0/sobjects/Account/0010C000002L2kzQAC"
      },
      "Id" : "0010C000002L2kzQAC",
      "Name" : "Parent Account"
    }
  }
}
| improve this answer | |

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.