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
LVSLVS 

Foreign keys in upsert operation

According to (www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm, see Rules and Guidelines section), the upsert operation supports use of External IDs in reference fields. That is,

I'm inserting an Opportunity and I need to specify the related Account. However, I have only the External Id value of Account. Usually, I would query the Accout's Id, the use it to insert a new Opportunity but the Salesforce API lets you skip this step - (see http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_upsert.htm)

However, I'm curious to know how this can be done via Apex code - the URL above talks about the Web Services API which is in Java or other environment code.

Has anyone been able to do that?

 

Best Answer chosen by LVS
Katia HageKatia Hage
You can perform the same in Apex, i.e. use a nested sObject to relate sObjects. This wasn't doc'd in the Apex dev guide even though it applies to Apex as well. 

This Apex topic shows how to insert parent and child records by using nested objects: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm

If your parent account records already exist, skip creating the second account in the example, and just create the opportunity.

All Answers

kiranmutturukiranmutturu

2nd link will be for the people who will use salesforce as an API to upsert records in to salesforce via c#, java....etc some other languages....

LVSLVS

Yes, that's what I said. I'm looking for an Apex equivalent.

Katia HageKatia Hage
You can perform the same in Apex, i.e. use a nested sObject to relate sObjects. This wasn't doc'd in the Apex dev guide even though it applies to Apex as well. 

This Apex topic shows how to insert parent and child records by using nested objects: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm

If your parent account records already exist, skip creating the second account in the example, and just create the opportunity.
This was selected as the best answer
LVSLVS
This is great! I'd forgotten I needed this :-)