function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kenji775Kenji775 

Handling a small CSV Importing using relationships and external IDs

Hey all.

I am building a small system that can import CSV files emailed to it. There are 3 objects involved, Accounts (a person account), and two custom objects, Auto, and Ownership. The import is responsible for creating ownership records (simply a junction object between an account and an auto).

 

So the Ownership has a master detail relationship with both Account and Auto. The account has a field called SSN, and the auto has a field called VIN. These are both marked as external IDs.

Account                   Ownership                              Auto
SSN  <----------  Account   |   Auto------------------> VIN

 

The question becomes how can I create an owernship record from my import file that contains a SSN and a VIN. Can you populate a lookup field with an external ID, or will I need to run a query first and populate the lookup relationships with the Id's from the query? I'd like to avoid having to query to save on performance and potential governor limits, but I know that may not be doable. In that case I'd like just build a set of SSN and VIN and do one query for each object type using the IN() SOQL operator correct?

Thanks for any insight! 

Best Answer chosen by Admin (Salesforce Developers) 
precursiveprecursive

Have you tried using relationships?

 

new bridgeObject(lookup__r.ExternalId = 'xyz', lookup2__r.externalId = 'abc');

 

upsert bridgeObject;

 

or 

 

ref1 = new object(externalId__c = 'xyz');

 

new bridgeObject(lookup__r = ref1);

 

upsert bridgeObject;

 

I have had mixed success with those approaches. 

All Answers

precursiveprecursive

Have you tried using relationships?

 

new bridgeObject(lookup__r.ExternalId = 'xyz', lookup2__r.externalId = 'abc');

 

upsert bridgeObject;

 

or 

 

ref1 = new object(externalId__c = 'xyz');

 

new bridgeObject(lookup__r = ref1);

 

upsert bridgeObject;

 

I have had mixed success with those approaches. 

This was selected as the best answer
Kenji775Kenji775

Cool, thank you, that is the kind of info I was looking for. I didn't know if you could populate a lookup with external id data and have it work. I know I could have just tried, but I know I won't get it right the first few times, so I wanted to make sure it could be done so I don't give up :P Thank you!