Moving data between two Salesforce Orgs can happen in the context of refreshing a Full Copy Sandbox instance with data from the Production instance. However, there are cases where data has to be moved from one Sandbox instance into another, or from a Production instance into a Config/Developer Sandbox. In such cases, the trickiest part of migrating the data is maintaining the relationships (lookup or master-detail) between Objects.
For example, take the case of Opportunities and Accounts. Opportunity data exported from the source Salesforce Org. would have references to Account ID’s that are specific to the source Org. When trying to import this data into the destination Salesforce Org, those IDs would not exist and the Opportunities would therefore not be associated with their respective Accounts.
A combination of External Id fields and the Data Loader ‘Upsert’ command can however move data from one Salesforce Org to another while maintaining all relationships. Using the Opportunity and Account standard Objects as an example, the step-by-step process is as follows:
1) In the destination Organization, on the Parent Object (in this case, Account), create a new field of type Text and mark it as an ‘External Id’ field. Be sure to make the length of the field at least 18 characters (the size of the Salesforce ID field). Let’s call this new field on Account – ‘Source Acct ID’.
2) Export the Parent Object (in this case, Account) data from the source Organization into a csv file using Data Loader. Be sure to include the ‘Id’ field when exporting this data.
3) Insert the data from Step 2 into the destination Organization using Data Loader. In the mapping screen of Data Loader, map the ‘ID’ field from the csv file to the new field created in Step 1 (in this case, ‘Source Acct ID’).
4) Export the Child Object (in this case, Opportunity) data from the source Organization into a csv file using Data Loader. Be sure to include the relationship field (in this case ‘AccountID’) when exporting this data.
5) Upsert the data from Step 4 into the destination Organization using Data Loader. In Step 2a of the Data Loader, select ‘ID’ as the field to use for matching Opportunities. In Step 2b of the Data Loader, select the field created in Step 1 (in this case, ‘Source Acct ID’) as the field to match the parent object. In the next mapping step of the Data Loader, map the ‘AccountId’ column of the csv file to the ‘Account:Source Acct ID’ field. This will allow the Data Loader to associate each Opportunity record with its respective parent Account record. Note that the ‘AccountId’ column of the CSV file should NOT be mapped to the standard ‘AccountId’ field (which is what the Data Loader does by default with the ‘Auto-Match Fields to Columns’ option). Instead, it has to be mapped to the ‘Account:Source Acct ID’ field.
6) After finishing the data import, the new field created in Step 1 (‘Source Acct ID’) may be deleted as it was only necessary for the data import process.