Set Lookup Relationship via Name Pointing Field?

Question
Can a lookup relationship be set using the __r name pointing field when inserting a record? A well-known post by Bob Buzzard suggests that you can relate records by assigning a nested sObject with an external ID in the __r field. However, this has been debated, with some arguing that __r fields are purely references and do not support this type of assignment.
For example, does the following work to associate a Contact with an Account via an external ID?
insert new Contact(
FirstName = 'Bob',
LastName = 'Buzzard',
Account = new Account(External__c = 'key')
);Some claim this approach should not work because __r fields serve as relationship pointers rather than data entry fields. So, does Salesforce allow setting lookup relationships this way?
Answer
Yes, Salesforce supports setting lookup relationships using the __r reference field when the parent record is identified by an external ID. This is documented under “Relating Records by Using an External ID” and “Creating Parent and Child Records in a Single Statement Using Foreign Keys.”
Accelerate your career with CRS Info Solutions Salesforce online training. Gain expert insights, hands-on experience, and in-depth knowledge to master the Salesforce ecosystem with confidence!!!
To relate a record to its parent using an external ID, the parent object must have a custom field marked as an External ID. The parent sObject is then assigned to the lookup field as a nested sObject.
For example
This correctly associates a new Opportunity with an existing Account using an external ID:
Opportunity newOpportunity = new Opportunity(
Name = 'OpportunityWithAccountInsert',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(7)
);
Account accountReference = new Account(
MyExtID__c = 'SAP111111'
);
newOpportunity.Account = accountReference;
Database.SaveResult results = Database.insert(newOpportunity);Explanation: This Apex code creates a new Opportunity and associates it with an existing Account using an external ID. Instead of requiring the Account’s Salesforce ID, it assigns a reference to an Account object containing only the external ID field (MyExtID__c). When the Opportunity is inserted, Salesforce automatically resolves the relationship based on the external ID.
This approach is also valid for custom relationships using __r fields. The following example demonstrates that an object can be inserted while setting a lookup relationship through an external ID:
insert new MyObject__c(Parent__r = new Parent__c(External__c = '1'));Explanation: This code inserts a new MyObject__c record while setting its Parent__c lookup relationship using an external ID. Salesforce resolves the relationship by matching External__c = '1' to an existing Parent__c record.
This confirms that __r fields can be used to set relationships via external IDs, contrary to the belief that they only act as references after insertion.
Summing Up
Salesforce allows setting lookup relationships using __r fields when the parent record is identified by an external ID, enabling seamless parent-child associations without needing the parent’s Salesforce ID. This approach is officially documented and works for both standard and custom relationships, simplifying record insertion in a single statement. Despite initial skepticism, real-world tests confirm that __r fields are not just references but can also be used for foreign key-based assignments.
Boost Your Salesforce Career with Expert Training in Ahmedabad
Take the next step in your Salesforce career with specialized online training from CRS Info Solutions in Ahmedabad. Our in-depth courses are designed for both beginners and advanced learners, covering key areas such as Salesforce Administration, Development, and AI modules. Through real-world, project-based learning, you’ll develop the skills needed to excel in the Salesforce ecosystem.
Our training offers hands-on experience, personalized mentorship, and comprehensive resources, including detailed class notes, Salesforce Training in Ahmedabad certification guidance, and interview preparation. Led by experienced industry professionals, we ensure you are fully prepared to meet the demands of the Salesforce job market and become job-ready.
Don’t wait to unlock new career opportunities—enroll now and participate in our free demo session to kickstart your Salesforce journey!!!

