Here is my problem. I'm trying to establish a parent child relationship like the below and want to use the OBJECT reference and NOT the ID reference.

I have created a custom lookup field on Contact called MyAccount that looks up to Account and want to populate that field using the object reference.

Sobject c = Schema.getglobalDescribe().get('Contact').newSObject();
c.put('lastname', 'sobject test');
c.putSobject('myaccount__r', a);
insert c;

When I query the contact after it's being inserted, however the reference is NOT populated. It's showing as NULL. How to fix this? Again, I want to use parent record reference and NOT ID reference.

Note: this works for Standard fields - for example if I do something like. c.putSobject('Account', a) - this works perfectly

Thank you.

1 Answer 1


I haven't been able to reproduce the behavior you are claiming with using putSObject() using a standard relationship field. It'll compile and run, but when I re-query the inserted contact, the relationship field is null.

Also, from Set Relationship Via Name Pointing Field?, the blog post by Bob Buzzard referenced in that question, and my own experimentation, it does seem that this only works when you use a field on the parent object that's marked as being an external Id (and that external Id seems to be the only thing that can be set in the object reference).

From that, I have to conclude that what you're asking for is not currently possible (Winter '21, API v50.0)

You must log in to answer this question.

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