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?

2 Answers 2

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}

0
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"
    }
  }
}
0

You must log in to answer this question.

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