SOQL getting the grandparent of an account from contact
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?
apex soql
add a comment |
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?
apex soql
add a comment |
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?
apex soql
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?
apex soql
apex soql
asked Feb 4 at 16:47
Leorah SumarongLeorah Sumarong
295
295
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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}
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 at 17:00
Leorah, this probably is because either your variable is aList<Contact>
or is not calledc
. Would you please edit your question to show the complete code you are using?
– David Reed
Feb 4 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 at 17:14
add a comment |
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"
}
}
}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f249042%2fsoql-getting-the-grandparent-of-an-account-from-contact%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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}
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 at 17:00
Leorah, this probably is because either your variable is aList<Contact>
or is not calledc
. Would you please edit your question to show the complete code you are using?
– David Reed
Feb 4 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 at 17:14
add a comment |
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}
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 at 17:00
Leorah, this probably is because either your variable is aList<Contact>
or is not calledc
. Would you please edit your question to show the complete code you are using?
– David Reed
Feb 4 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 at 17:14
add a comment |
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}
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}
answered Feb 4 at 16:52
David ReedDavid Reed
36.2k72154
36.2k72154
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 at 17:00
Leorah, this probably is because either your variable is aList<Contact>
or is not calledc
. Would you please edit your question to show the complete code you are using?
– David Reed
Feb 4 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 at 17:14
add a comment |
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 at 17:00
Leorah, this probably is because either your variable is aList<Contact>
or is not calledc
. Would you please edit your question to show the complete code you are using?
– David Reed
Feb 4 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 at 17:14
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 at 17:00
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 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 at 17:08
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 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 at 17:14
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 at 17:14
add a comment |
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"
}
}
}
add a comment |
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"
}
}
}
add a comment |
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"
}
}
}
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"
}
}
}
answered Feb 4 at 18:01
Pranay JaiswalPranay Jaiswal
17k32755
17k32755
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f249042%2fsoql-getting-the-grandparent-of-an-account-from-contact%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown