0

I would like to dynamically fetch the parent Account of an Account in a similar fashion to the way it's done in the selected answer to this question. However, this doesn't seem to work in this case. The line right before the debug will throw a NullPointerException, no matter I use ParentId, Parent, Account, or ParentAccount.

Account topAccount = new Account(Name = 'top');
insert topAccount;
Account childAcc = new Account(Name ='Acme', Parent= topAccount);
insert childAcc;
Contact c1 =new Contact(LastName = 'T', AccountId = childAcc.Id);
insert c1;
SObject c2 = [select Account.Parent.Name from Contact where ID =:c1.ID];
String topAccName = (String)c2.getSObject('Account').getSObject('ParentId').get('Name');
System.debug('****Top acc name: ' + topAccName);
0

Relationships between objects are established by setting ID values so you need ParentId = topAccount.Id. And in your topAccName assignment you were using c1 which will not have any relationship fields set; those are only set when the related objects are queried together.

This does not cause any NPEs:

Account topAccount = new Account(Name = 'top');
insert topAccount;
Account childAcc = new Account(Name ='Acme', ParentId = topAccount.Id);
insert childAcc;
Contact c1 =new Contact(LastName = 'T', AccountId = childAcc.Id);
insert c1;

SObject c2 = [select Account.Parent.Name from Contact where ID =:c1.ID];
String topAccName = (String) c2.getSObject('Account').getSObject('Parent').get('Name');
System.debug('****Top acc name: ' + topAccName);
3
  • Thanks! I chose your answer as correct, since the using ParentId was the issue, but edited my question to remove the c2/contact issue, since that was just a copy-paste/edit issue. – ceiroa Aug 13 '15 at 21:10
  • Would you care to elaborate on why relationships between objects are established by setting ID values? I can see it works like that, but don't fully grok why (or where exactly in the documentation this is laid out). – ceiroa Aug 13 '15 at 21:19
  • 1
    @ceiroa I'm not sure how well this is explained in documentation. Best to remember that underneath it's a database and relationships in databases are established by FK/PK relationships. The relationship references (a bit clearer with custom objects where they end in __r) are established in queries and while they can be programmatically set are not considered when objects are inserted or updated. – Keith C Aug 13 '15 at 21:23
0

If you are trying to print the Parent Account Name from the Contact object, then you have to do this

SObject contact = [select Account.Parent.Name from Contact where ID =:c1.ID];
String topAccName = contact.Account.Parent.Name;

but its safe to add null checks since there could be contact's account without a parent

I am not sure about this line

(String)c1.getSObject('Account').getSObject('ParentId').get('Name');
System.debug('****Top acc name: ' + topAccName);

even if you are doing it like this, you need to replace c1 variable (after you insert c1, only id will be retained in that variable) with contact variable (the one you queried ) and try

(String)contact.getSObject('Account').getSObject('Parent').get('Name');
System.debug('****Top acc name: ' + topAccName);

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.