Find your content:

Search form

You are here

How to connect Salesforce to Salesforce in Apex


Is it possible to connect Salesforce to Salesforce in Apex. I want to bring sample records from Full Copy sandbox to config sandbox and I need to do this as I refresh config sandbox.

I think if we can connect to another sandbox via apex we can automate the process of manual data load.

Attribution to: Prafulla Patil

Possible Suggestion/Solution #1

This sounds like a great use of Salesforce to Salesforce if you don't mind doing all the config to setup the connection. Salesforce to Salesforce only lets you connect sandbox orgs to other sandbox orgs, and production/dev orgs to other production/dev orgs, but there is no limitation for connecting two sandboxes related to the same production org to each other.

Once the config is setup you can use the PartnerNetworkRecordConnection object to automate the sharing.

Let us know how this works out for you!

Attribution to: Ralph Callaway

Possible Suggestion/Solution #2

I agree with PepeFloyd -- If you're just looking to copy data then you could use an app like SFXOrgData ( to do this. I developed this application specifically for this purpose. It lets you connect to your source org (could be Sandbox or Production) and your target org (again could be Sandbox or Production). I do like the idea of using all native APEX code (while leveraging the SOAP API) to do this, but I think there are a bunch of governor limit's you'll hit if your copying a lot of records.

Attribution to: Kashyap Patel

Possible Suggestion/Solution #3

The only way to access your data in that way would be to expose it via the SOAP API.

However, you cannot connect to these APIs from APEX.

Since the data doesn't come into your config only sandbox it is kind of an issue for getting data in there. Your also limited on data size to 500mb in config-only orgs.

You might consider:

  • Outside program that has access to both orgs
  • The SFDC data-loader


Perhaps check out: APEX-Metadata API seems promising for your needs. I guess I assumed the external APIs were black-listed from internal domain requests. This apparently is not the case.

Attribution to: jordan.baucke

Possible Suggestion/Solution #4

This cookbook article, Calling Salesforce Web Services Using Apex shows how to import the Partner API into Apex. You can then call the query and insert operations on that service to communicate with another org. Let me know if this is something you want to explore, I've been having some success with getting the SF SOAP API's working from Apex recently! ;-)

SOAP API Developer's Guide

Attribution to: Andrew Fawcett

Possible Suggestion/Solution #5

I reckon Salesforce 2 Salesforce cannot be enabled for two orgs within the same salesforce organisation (I.e. two sandboxes under the same production org) It can only be enabled across two different (partner) orgs, I believe even across two Developer orgs would work.

Besides, a full copy sandbox and a config sandbox will have significant differences in data storage limit.

I've seen a tool like Monarch from Dreamfactory used to seamlessly move data between sandboxes.

The other alternative will be using the Apex DataLoader to move and externalId fields to join up records.

Attribution to: techtrekker

Possible Suggestion/Solution #6

If all you want is to move data for testing purposes, there is no need for apex. You can do this using Talend (free), Boomi or Informatica Data Loader.

Attribution to: PepeFloyd

Possible Suggestion/Solution #7

I would take a look here for some direction:

You can expose apex classes/methods that will let you query for the data in an SF system via SOAP or Rest - but you will need to handle the authentication (the article above discusses that at length).

The other challenge you have when moving between systems is re-creating the relationships in the target system e.g. associating Contacts with Accounts, since the AccountIds will be different in the target system. So when you create the new contacts, you need to find their parent accounts, and link them to the new AccountId of their parent account. The easiest way in my experience is to create an external Id field called LiveId, populate that field with the Id from the live system, and then use native upsert to correctly associate the child records with their parent in the new system.

Attribution to: BritishBoyinDC
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content