Developers / Admins / Data

Salesforce to Salesforce Integration Using OAuth 2.0: A Comprehensive Guide

By Parameswara Reddy Palle

In today’s fast‐paced business environment, organizations need to share data, collaborate on opportunities, and align processes across distinct Salesforce instances. Salesforce-to-Salesforce integration using OAuth 2.0 provides a secure, scalable method to achieve seamless data retrieval and updates between Salesforce orgs.

This article offers a detailed, step-by-step guide for implementing Salesforce-to-Salesforce integration using OAuth 2.0. Whether you’re a seasoned Salesforce Admin or new to cross-org integrations, these insights and real-world best practices will help you unlock a more seamless and secure data-sharing ecosystem.

Why Use OAuth 2.0 for Salesforce-to-Salesforce Integration?

OAuth 2.0 offers several advantages for cross-org integrations:

  • Enhanced Security: OAuth 2.0 provides robust authentication, ensuring that sensitive business data is exchanged securely between orgs.
  • Simplified Authentication: With named credentials and auth providers, managing access tokens and API callouts becomes much easier.
  • Improved Operational Efficiency: Streamlined configuration means reduced setup times and lower learning curves for admins.
  • Real-Time Data Collaboration: Asynchronous API calls and platform events allow for near real-time updates across orgs, supporting dynamic business processes.

These benefits make OAuth 2.0 the preferred method for implementing Salesforce-to-Salesforce integrations.

Setting Up Salesforce-to-Salesforce Integration

Implementing the integration involves several high-level steps. In a typical setup, you would:

  • Enable and Configure Integration in Your Org: Activate Salesforce-to-Salesforce in Setup and customize sharing settings, connection policies, and security rules.
  • Establish Connections: Send a connection invitation to your partner organization and accept it using enhanced tracking and validation features.
  • Define Field Mapping and Data Sharing Rules: Use the mapping tool to align data fields (leads, opportunities, or custom objects) between orgs, ensuring that only selected data is shared.
  • Leverage Advanced Integration Options: Utilize asynchronous API calls and platform events for real-time updates, particularly in scenarios that demand immediate data synchronization.
  • Test and Monitor: Always test your integration in a sandbox environment before going live, using built-in monitoring tools to track data flows and manage errors.

Prerequisites

Before you begin, make sure you have:

  • Two Salesforce Orgs: One serving as the source and the other as the target.
  • Administrative Access: Admin rights in both orgs.
  • Enabled API Access: Ensure APIs are activated in both orgs.

Use Case Highlights

Before diving into the implementation steps, here are some key use case highlights for this integration:

  • Create and Edit Accounts in the Target Org: Seamlessly retrieve, create, and update account records in the target org to streamline your business processes across Salesforce environments.
  • Full Code Availability: For the complete source code and implementation details, please visit the GitHub repository.

Step-by-Step Guide: Implementing Integration with OAuth 2.0

For a secure and robust Salesforce-to-Salesforce integration, OAuth 2.0 is the recommended authentication mechanism. Follow these steps to connect your Salesforce orgs using OAuth 2.0:

Step 1: Create a Connected App in the Target Org

  • Navigate to Setup → App Manager.
  • Click “New Connected App”.
  • Fill in basic information:
    • Name: test salesforce integration
    • API Name: test_salesforce_integration
    • Contact Email: your-email@example.com
  • Enable OAuth Settings:
    • Check “Enable OAuth Settings”.
    • Callback URL: https://login.salesforce.com/services/oauth2/callback (a dummy URL initially).
    • Select OAuth Scopes:
      • Full access (full)
      • Perform requests on your behalf at any time (refresh_token, offline_access)
  • Save and continue:
    • Copy the Consumer Key and Consumer Secret for use in the source org.

Step 2: Create an Auth Provider in the Source Org

  • Navigate to Setup and search for “Auth Provider”.
  • Click “New”.
  • Select Salesforce as the Provider Type.
  • Enter details:
    • Name: salesforce connect
    • URL Suffix: salesforce_connect
    • Consumer Key and Secret: Use values from Step 1.
    • Authorize Endpoint URL: https://login.salesforce.com/services/oauth2/authorize
    • Token Endpoint URL: https://login.salesforce.com/services/oauth2/token
  • Save and copy the generated callback URL.
  • Update the connected app:
    • Return to your connected app in the target org, edit it, and add the copied callback URL.

Step 3: Create a Named Credential in the Source Org

  • Navigate to Setup and search for “Named Credentials”.
  • Click “New”.
  • Provide details:
    • Label and Name: testConnection
    • URL: https://your-target-org.my.salesforce.com (your target org’s My Domain URL)
    • Identity Type: Named Principal
    • Authentication Protocol: OAuth 2.0
    • Auth Provider: Select the Auth Provider from Step 2.
  • Save: Log in to the target org when prompted to complete the configuration.

Step 4: Create an HTTP Callout Class in the Source Org

Develop an Apex class (e.g. AccountHelper.cls) to handle HTTP callouts. This class should include methods to retrieve and update account data from the target org.

Additional Component: Consider creating a Lightning Web Component (LWC), such as getAccountDetailsIfRequiredSaveAccountDetailsToo, to display account information dynamically. Please add it to the home screen.

Step 5: Create a REST Apex Class in the Target Org

In the target org, create a REST API (e.g. using RestApiUsingConnectClass.cls) to process account operations. This class exposes endpoints for creating, updating, and fetching account records.

Testing the Integration

  • Retrieve accounts:
    • Execute AccountHelper.getAccountList() in the Developer Console of the source org and verify the retrieved account details.
  • Create or update accounts:
    • Run AccountHelper.createOrUpdateAccount() with sample account details and confirm the changes in the target org.

Troubleshooting and Debugging

  • Unauthorized Errors: Ensure the connected app in the target org is approved and that Auth Provider and Named Credential settings are correct.
  • API Call Failures: Check Debug Logs for error messages, verify the endpoint URL in Named Credentials, and confirm OAuth scopes include both full and refresh_token.

Highlights and Use Cases

Salesforce-to-Salesforce integration using OAuth 2.0 delivers significant benefits:

  • Data Security: With granular sharing controls and robust authentication, your sensitive business data remains protected.
  • Real-Time Collaboration: Asynchronous processing and platform events enable real-time updates, ideal for dynamic sales environments.
  • Operational Efficiency: Streamlined configuration and a modern UI reduce setup times and ease administration.
  • Scalability: Whether integrating with a partner, aligning regional offices, or connecting subsidiary systems, the integration scales effortlessly with your business needs.

These highlights demonstrate why many organizations choose this approach to drive better decision-making and enhance customer experiences.

Final Thoughts

Salesforce-to-Salesforce integration using OAuth 2.0 is a game-changer – combining enhanced security, flexible data mapping, and streamlined authentication to ensure seamless data exchange and collaboration between your Salesforce orgs.

Experiment with these configurations, share your experiences in the comments, and continue to refine your integration strategy to fully leverage the power of Salesforce.

The Author

Parameswara Reddy Palle

Parameswara is a Salesforce Developer with three years of experience specializing in B2B Commerce, Service Cloud, Apex, LWC, and Flows. He is 4x Salesforce certified.

Comments:

    Mahesh
    April 14, 2025 4:29 pm
    Without code how can we try this

Leave a Reply