Integration of Hubspot with Salesforce (Part 1)

Are you ready to supercharge your business by connecting two powerful tools: HubSpot and Salesforce? In this beginner-friendly guide, we'll explain how to import contacts from HubSpot into Salesforce. Don't worry if you're new to these platforms; we'll take it step by step.

Why Connect HubSpot and Salesforce?

HubSpot and Salesforce are like superheroes for your business. HubSpot helps you with marketing and leads, while Salesforce is your go-to for managing customers and sales. When you connect them, magic happens:
No More Manual Work: You won't need to copy contact info from HubSpot to Salesforce by hand. It happens automatically!

Step 1: HubSpot Setup

First, let's get HubSpot ready:

  • Create a HubSpot Account: If you don't have one, sign up for a HubSpot account at HubSpot's website.
  • Make a Private App: This sounds fancy, but it's just telling HubSpot you want to connect with Salesforce. HubSpot will give you a special key for this.
  • Get Your Key: With your private app, you'll get an access key. This key is like a secret code that lets Salesforce talk to HubSpot. Keep it safe!

Step 2: Salesforce Setup

Now, let's dive into Salesforce:

  • Open Salesforce Developer Console: If you're new to Salesforce, this is where you write special code (don't worry, it's easier than it sounds).
  • Create an Apex Class: In Salesforce, we write a special program called an "Apex class" to connect with HubSpot. Think of it like teaching Salesforce a new trick.
  • Define Constants: Inside your Apex class, we tell Salesforce the web address to talk to HubSpot and give it the secret key we got earlier. This is done with some special words like this:
private static final String HUBSPOT_API_URL = 'https://api.hubapi.com/crm/v3/objects/contacts';

//you can get api from Hubspot Developer Documentation Link: https://developers.hubspot.com/docs/api/crm/contacts

private static final String HUBSPOT_ACCESS_TOKEN = 'YOUR_HUBSPOT_ACCESS_TOKEN'; 
Remember to replace 'YOUR_HUBSPOT_ACCESS_TOKEN' with your real secret key.

dont miss out iconDon't forget to check out: How to Integrate HubSpot With Salesforce CRM: Three Options

Step 3: Define Contact Data

In our Apex class, we create a plan for the contact data we want from HubSpot. We use these special words:

public class HubSpotContactProperties {
     public String firstname;
     public String lastname;
     public String email;
 // ... add other properties ...
}
public class HubSpotContact {
     public HubSpotContactProperties properties;
}
public class HubSpotContactsResponse {
     public List<HubSpotContact> results;
}
  • HubSpotContactProperties: These are like labels for the contact info we want, like names and email addresses.
  • HubSpotContact: This is like a container that holds a contact's info.
  • HubSpotContactsResponse: This is like a report that HubSpot gives us with all the contacts.

Step 4: Importing Contacts into Salesforce from HubSpot

This section includes the core of our integration - importing contacts from HubSpot into
Salesforce.

@AuraEnabled
public static void importContacts() {
     // Get contacts from HubSpot
     List<HubSpotContact> hubSpotContacts = getHubSpotContacts();
     // Create or update contacts in Salesforce
     List<Contact> contactsToInsert = new List<Contact>();
     List<Contact> contactsToUpdate = new List<Contact>();
     for (HubSpotContact hubSpotContact : hubSpotContacts) {
         HubSpotContactProperties properties = hubSpotContact.properties;
         Contact salesforceContact = new Contact();
         salesforceContact.FirstName = properties.firstname;
         salesforceContact.LastName = properties.lastname;
         salesforceContact.Email = properties.email;
         // Check if contact already exists in Salesforce
         List<Contact> existingContacts = [SELECT Id FROM Contact WHERE Email = 
         :salesforceContact.Email];
         if (existingContacts.isEmpty()) {
              contactsToInsert.add(salesforceContact);
         } 
         else {
              salesforceContact.Id = existingContacts[0].Id;
              contactsToUpdate.add(salesforceContact);
         }
 }
 insert contactsToInsert;
 update contactsToUpdate;
}
  • @AuraEnabled: This annotation makes the method accessible for use in Salesforce Lightning components.
  • We use the getHubSpotContacts method to fetch contacts from HubSpot.
  • Then, we create two lists: contactsToInsert for new contacts and contactsToUpdate for existing ones.
  • We loop through the HubSpot contacts, extract their properties, and map them to corresponding Salesforce Contact fields (like first name, last name, and email).
  • We check if a contact already exists in Salesforce based on their email address. If it doesn't exist, we add it to contactsToInsert; otherwise, we update it in contactsToUpdate.
  • Finally, we insert new contacts and update existing ones in Salesforce.

Part 4: Fetching Contacts from HubSpot

This section defines the method responsible for making the actual HTTP request to HubSpot to fetch contacts.

private static List<HubSpotContact> getHubSpotContacts() {
     HttpRequest request = new HttpRequest();
     request.setEndpoint(HUBSPOT_API_URL);
     request.setMethod('GET');
     request.setHeader('Authorization', 'Bearer ' + HUBSPOT_ACCESS_TOKEN);
     Http http = new Http();
     HttpResponse response = http.send(request);
     HubSpotContactsResponse responseBody = (HubSpotContactsResponse) 
     JSON.deserialize(response.getBody(), HubSpotContactsResponse.class);
     return responseBody.results;
}
  • getHubSpotContacts is a private method that sends an HTTP request to the HubSpot API using the URL and access token we defined earlier.
  • It sets up the request, including the method (GET) and authorization header with the access token.
  • Then, it uses the Http class to send the request to HubSpot.
  • The response is received and deserialized from JSON into a structured format using JSON.deserialize.
  • Finally, it returns the list of HubSpot contacts from the response.

dont miss out iconCheck out another amazing blog here by Ayfaz: Introduction to Lightening Web Components | The Ultimate Guide

In the next part, we'll explore importing contacts from Salesforce Contact objects into HubSpot Contact objects.

Responses

Popular Salesforce Blogs