top of page

Building a Custom Integration between Sales Cloud and Marketing Cloud

The integration between Sales Cloud and Marketing Cloud is essential for many businesses looking to create a unified view of their customers. It enables marketers to target specific audiences more effectively, while sales teams can leverage insights to close more deals.

The integration between Sales Cloud and Marketing Cloud is essential for many businesses looking to create a unified view of their customers. It enables marketers to target specific audiences more effectively, while sales teams can leverage insights to close more deals. In this blog post, we will explore the step-by-step process of building a custom integration between these platforms.

Step 1: Define Your Integration Requirements

Before diving into the technical details, it's crucial to outline the objectives of your integration. What data needs to be shared? How often will synchronization occur? What direction will data flow? These are all critical questions that need answers.


- Identify Data Entities: Outline what objects (leads, contacts, campaigns) will be shared.

- Determine Integration Flow: Decide if data will flow one-way or bi-directionally.

Step 2: Setup API Access

API access setup is foundational to your integration. Here's how to do it:

2.1For Sales Cloud:

2.1.1. Create a Connected App Connected Apps define the method of integration and provide the necessary credentials to connect to the Salesforce instance. Here's how to create a Connected App:

  • Navigate to Setup: Log in to your Salesforce organization, and click 'Setup' in the upper right corner.

  • Search for App Manager: In the Quick Find box, type 'App Manager' and select it.

  • Create New Connected App: Click 'New Connected App', and you'll see a form that requires specific information.

  • Fill in Details: Provide the necessary details, including the name, contact email, and logo (if needed). Also, specify the required OAuth scopes, which define the permissions granted to the connected app.

  • Configure Callback URL: You must provide a Callback URL, which is used for OAuth authentication. It's where the access token will be sent after successful authentication.

  • Set OAuth Policies: Choose the OAuth policies, such as the token expiration time, depending on your integration needs.

  • Save and Wait for Propagation: After clicking 'Save', it may take a few minutes for the changes to propagate throughout Salesforce's system.

2.2.2. Authenticate Using OAuth 2.0 OAuth 2.0 is a protocol that allows secure authorization in a simple and standardized way. Here's how to authenticate using OAuth 2.0:

  • Obtain Consumer Key and Secret: From the Connected App details, retrieve the 'Consumer Key' and 'Consumer Secret'. These are vital for OAuth 2.0 authentication.

  • Perform OAuth 2.0 Authorization Flow: Depending on the nature of your integration, choose the appropriate OAuth 2.0 grant type. Typically, the 'Authorization Code Grant' or 'Client Credentials Grant' is used for server-to-server integrations.

  • Request Access Token: Use the consumer key, consumer secret, and other relevant details to request an access token from Salesforce's token endpoint.

  • Store Access Token Securely: Once you have the access token, store it securely in your system. This token will be used to make authorized API requests to Sales Cloud.

2.2For Marketing Cloud:

2.2.1. Create an Installed Package: An Installed Package acts as a container for various API integration components within Marketing Cloud. It's a way to bundle everything required for the integration into one package. Here's how to create it:

a. Navigate to Setup in Marketing Cloud.

b. Under Platform Tools, click on Apps > Installed Packages.

c. Click the ‘New’ button to create a new Installed Package.

d. Provide a name and description that will help you identify this specific integration.

e. Save the Installed Package, and you’ll be taken to the package's detail page.

2.2.2. Create API Integration Component: Inside the Installed Package, you must define the specific API integration.

a. Click ‘Add Component’ in the Installed Package detail page.

b. Select the integration type, like ‘Server-to-Server’.

c. Define the permissions this integration will have. It’s important to follow the principle of least privilege, granting only the permissions necessary for your integration to function.

d. Save the component, and you'll be presented with the ‘Client ID’ and ‘Client Secret’. Keep these credentials securely, as they will be required to authenticate your requests to Marketing Cloud.

2.2.3. Authenticate Using OAuth 2.0: OAuth 2.0 is a standard protocol for authorization, and Marketing Cloud uses it to authenticate requests from external systems.

a. Obtain the ‘Client ID’ and ‘Client Secret’ from the previous step.

b. Use these credentials to request an access token from Marketing Cloud's authentication endpoint.

c. Include the access token in the HTTP header of your subsequent API calls to authenticate them.

Here's an example of obtaining an access token using a POST request in cURL:


curl -X POST \ \
  -H 'Content-Type: application/json' \
  -d '{
    "clientId": "YOUR_CLIENT_ID_HERE",
    "clientSecret": "YOUR_CLIENT_SECRET_HERE"

Replace YOUR_CLIENT_ID_HERE and YOUR_CLIENT_SECRET_HERE with your actual credentials. Setting up API access in Marketing Cloud is a three-step process involving the creation of an Installed Package, the addition of an API integration component, and authentication using OAuth 2.0. By following this process, you're establishing a secure and controlled way for Sales Cloud to communicate with Marketing Cloud, laying the groundwork for your custom integration.

Step 3: Develop Your Integration Logic

The core of your integration lies in the logic you develop for synchronizing data.

3.1. Create Apex Classes

In this phase, you'll develop specific Apex classes that handle communication with both Sales Cloud and Marketing Cloud. You'll need separate classes to manage Sales Cloud and Marketing Cloud interactions.

Sales Cloud Integration Class:

Here, you'll create methods to perform CRUD operations on Sales Cloud objects that need to be shared with Marketing Cloud.

For example:

  • Method to Retrieve Leads: Use REST or SOAP API to fetch leads from Sales Cloud that meet specific criteria.

  • Method to Update Contacts: If changes are made in Marketing Cloud, reflect those updates in Sales Cloud.

Sample code snippet for fetching leads:

public class SalesCloudIntegration {
    public static List<Lead> getLeads() {
        // Authentication logic
        // REST API call to fetch leads
        // Return list of leads

Here's a snippet of code that can be used as a template for making REST API calls to Sales Cloud:


public class SalesCloudIntegration {
    public static HttpResponse makeRequest(String endpoint, String method, String body) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');

        Http http = new Http();
        HttpResponse res = http.send(req);

        return res;

Marketing Cloud Integration Class:

This class will manage interactions with Marketing Cloud. Similar to Sales Cloud, you'll need methods to create, read, update, and delete relevant entities.

Sample code snippet for updating contacts in Marketing Cloud:

public class MarketingCloudIntegration {
    public static void updateContacts(List<Contact> contacts) {
        // Authentication logic
        // REST API call to update contacts in Marketing Cloud

The code structure for Marketing Cloud integration is similar:


public class MarketingCloudIntegration {
    public static HttpResponse makeRequest(String endpoint, String method, String body) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer YOUR_ACCESS_TOKEN');

        Http http = new Http();
        HttpResponse res = http.send(req);

        return res;

3.2. Data Synchronization Logic

This phase deals with the rules governing data synchronization between Sales Cloud and Marketing Cloud. You need to develop logic to determine how and when data will be transferred between the two.

  • Batch Processing: If you have a large volume of data, consider processing records in batches.

  • Real-time vs Scheduled Sync: Determine if synchronization will occur in real-time or on a schedule.

  • Mapping Between Objects: You'll need to ensure that data is correctly mapped between corresponding objects in Sales Cloud and Marketing Cloud.

3.3. Error Handling and Logging

Robust error handling ensures that your integration can deal with unexpected issues without breaking down.

  • Exception Handling: Implement try-catch blocks to gracefully handle exceptions.

  • Logging: Log errors and important events to facilitate troubleshooting.

  • Notification System: Consider setting up notifications to alert you or your team when an error occurs.

Step 4: Test Your Integration

Testing is a critical phase in any development project, and it's especially vital when building an integration between complex systems like Sales Cloud and Marketing Cloud. It helps you identify errors and ensure that the data is being transferred correctly between the two platforms.

4.1. Build Test Cases Objective: Create comprehensive test cases to cover all possible scenarios and edge cases.

  • Identify Test Scenarios: Outline all possible cases, including success paths, failure paths, boundary cases, and more.

  • Write Automated Tests: If possible, write automated tests using tools like Apex Test Framework, making your testing process more efficient and repeatable.

4.2. Use a Sandbox Environment Objective: Perform initial testing in a controlled, isolated environment.

  • Create a Sandbox: Clone your production environment to a sandbox where you can safely test without affecting live data.

  • Mimic Real-World Scenarios: Populate the sandbox with sample data that closely resembles real-world data to ensure accuracy in testing.

4.3. Involve Stakeholders Objective: Engage key stakeholders for user acceptance testing (UAT).

  • Gather Feedback: Include users who will be working with the integration daily to provide real-world insight and ensure the integration meets their needs.

4.4. Perform End-to-End Testing Objective: Test the entire integration flow.

  • Test Data Flow: Validate that the data flows correctly between Sales Cloud and Marketing Cloud, honoring all transformation rules, data mappings, and business logic.

  • Check Error Handling: Test how the system behaves under failure conditions and ensure appropriate error handling.

Testing is not just about finding bugs; it's about validating that the integration meets the business requirements, complies with data quality standards, and performs efficiently under real-world conditions. Investing time and resources into thorough testing will save much more in terms of future maintenance and user satisfaction.

Step 5: Deploy and Monitor

Deploying and monitoring your integration is a crucial phase in the development process. It ensures that your code runs smoothly in the production environment and allows for ongoing oversight to catch and resolve any issues that might arise. This step can be divided into several sub-steps, each of which contributes to a successful deployment and continuous monitoring.

5.1. Deployment to Production Prepare for Deployment:

  • Review Your Code: Before deploying, review your code thoroughly to ensure that it adheres to best practices and that all testing has been completed successfully.

  • Create Deployment Plan: Outline the steps needed for deployment, including any dependencies that must be in place beforehand.

  • Choose the Right Environment: Ensure that the production environment matches the configuration of your testing environment to prevent any unexpected issues.


  • Migrate the Code: Utilize Salesforce's change set or any third-party Continuous Integration (CI) tools to move your code from the development or staging environment to production.

  • Validate the Deployment: After deployment, validate that the integration is functioning as expected by checking logs and conducting initial tests.

5.2. Set Up Monitoring Continuous monitoring is vital for maintaining the health and performance of your integration.

  • Utilize Monitoring Tools: Salesforce provides various tools for monitoring, such as Health Check and Event Monitoring. Choose the appropriate tools to oversee your integration's performance and stability.

  • Create Alerts: Set up automated alerts that notify you of any issues. This proactive approach ensures that you can react quickly to any problems that might arise.

  • Analyze Logs Regularly: Regularly review the logs to catch any errors or unusual behavior. Look for trends that might indicate underlying issues.

5.3. Provide Support and Maintenance Ongoing support and maintenance are essential for the long-term success of your integration.

  • Create Support Documentation: Write detailed documentation that outlines how to troubleshoot common issues and outlines the structure of your integration. This documentation can assist both your team and any future developers.

  • Plan for Regular Updates: As Salesforce releases updates or as your business needs change, you'll need to make adjustments to your integration. Plan for regular updates and testing to ensure ongoing compatibility.

  • Engage with Stakeholders: Keep communication lines open with the relevant stakeholders, providing them with updates and being available to address any concerns.

Building a custom integration between Sales Cloud and Marketing Cloud requires careful planning, robust development, and ongoing maintenance. By following this guide, you can build a strong and resilient integration that aligns with your business goals.

Remember to consult the official Salesforce and Marketing Cloud API documentation for specific information on endpoints, methods, and authentication. Your specific integration might require customization beyond what's covered here, so adapt the provided guidelines to fit your unique requirements.

Further Reading

By integrating these two powerful platforms, you'll enhance collaboration between your marketing and sales teams, streamline your processes, and ultimately drive more revenue for your business.

Happy integrating! 🚀

19 views0 comments


bottom of page