Welcome back to the second part of our HubSpot-Salesforce integration journey! In this
installment, we’ll explore the other side of the integration equation: importing contacts from
Salesforce Contact objects into HubSpot Contact objects.
In Part 1 of this series, we learned how to fetch contacts from HubSpot and bring them into
Salesforce. Now, it’s time to complete the loop by ensuring that data flows seamlessly in both
directions.
Why Import Contacts from Salesforce into HubSpot?
Integrating Salesforce and HubSpot bidirectionally offers several advantages:
Holistic Customer View: You get a 360-degree view of your contacts, whether they
originated in Salesforce or HubSpot.
Efficient Marketing: HubSpot’s marketing features can target Salesforce contacts with
personalized campaigns.
Streamlined Sales: Salesforce users can access the latest HubSpot contact data for more
effective sales interactions.
Now, let’s dive into the technical details of importing Salesforce contacts into HubSpot using
the provided Apex class, HubSpotIntegration.
Apex Class Overview
Below is the HubSpotIntegration Apex class that handles the import process:
public class HubSpotIntegration { // Method to create HubSpot contacts from Salesforce contacts public static void createHubSpotContacts() { // HubSpot API endpoint for batch contact creation String endpoint = 'https://api.hubapi.com/crm/v3/objects/contacts/batch/create'; // Replace 'YOUR_HUBSPOT_API_KEY' with your actual HubSpot API key String apiKey = ''; // Query to fetch all Salesforce contacts List<Contact> salesforceContacts = [SELECT Id, FirstName, LastName, Email FROM Contact]; // Prepare the request payload Map<String, Object> payload = new Map<String, Object>(); List<Map<String, Object>> contactObjects = new List<Map<String, Object>>(); // ... (Continued below) } // Helper method to check if a contact already exists in HubSpot based on email // ... }
HubSpotIntegration: This is the name of the Apex class responsible for the integration
between Salesforce and HubSpot.
createHubSpotContacts(): This method is used to create HubSpot contacts from
Salesforce contacts. It’s the heart of our integration.
endpoint and apiKey: These variables store the HubSpot API endpoint and your HubSpot
API key. Replace ‘YOUR_HUBSPOT_API_KEY’ with your actual key.
salesforceContacts: This list holds the Salesforce contacts we want to import into
HubSpot.
Prepare Request Payload
In this section, we’ll look at how we prepare the payload that will be sent to HubSpot.
// Prepare the request payload Map<String, Object> payload = new Map<String, Object>(); List<Map<String, Object>> contactObjects = new List<Map<String, Object>>(); // Loop through Salesforce contacts for (Contact sfContact : salesforceContacts) { // Check if the contact already exists in HubSpot if (!contactExistsInHubSpot(sfContact.Email, apiKey)) { // Create a map to hold contact properties Map<String, Object> contactProperties = new Map<String, Object>(); // Set properties for the contact (firstname, lastname, email, etc.) contactProperties.put('firstname', sfContact.FirstName); contactProperties.put('lastname', sfContact.LastName); contactProperties.put('email', sfContact.Email); // Add more properties as needed // Create a contact object in the expected format for HubSpot Map<String, Object> contactObject = new Map<String, Object>{ 'properties' => contactProperties, 'associations' => new List<Object>() }; // Add the contact object to the list contactObjects.add(contactObject); } } // Add the list of contact objects to the payload payload.put('inputs', contactObjects); // Serialize the payload to JSON format String payloadJson = JSON.serialize(payload);
Payload and contactObjects: These variables are used to construct the payload that will
be sent to HubSpot. It’s essentially a list of contact objects.
Loop Through Salesforce Contacts: We loop through each Salesforce contact.
contactExistsInHubSpot(): We check if the contact already exists in HubSpot based on
their email address. If it doesn’t exist, we proceed to create a HubSpot contact.
contactProperties: We create a map to hold the contact’s properties, such as first name,
last name, and email. You can add more properties as needed
contactObject: We create a contact object in the format expected by HubSpot, including
properties and associations.
contactObjects.add(contactObject): We add the contact object to the list of contact
objects.
Finally, we assemble the payload and serialize it to JSON format for sending to HubSpot.
Send HTTP Request and Check Response
In this final section, we send the HTTP request to HubSpot and check the response.
// Set up headers with API key and content type Map<String, String> headers = new Map<String, String>{ 'Authorization' => 'Bearer ' + apiKey, 'Content-Type' => 'application/json' }; // Create an HTTP request to send the payload to HubSpot HttpRequest request = new HttpRequest(); request.setEndpoint(endpoint); request.setMethod('POST'); request.setBody(payloadJson); // Set request headers for (String key : headers.keySet()) { request.setHeader(key, headers.get(key)); } // Send the HTTP request to create contacts in HubSpot HttpResponse response = new Http().send(request); // Check the response if (response.getStatusCode() == 200) { // Contacts successfully created in HubSpot System.debug('Contacts successfully created in HubSpot.'); } else { // Failed to create contacts, log the status code and response body System.debug('Failed to create contacts. Status code: ' + response.getStatusCode()); System.debug('Response body: ' + response.getBody()); }
headers: We set up the headers for the HTTP request, including the authorization
header with your HubSpot API key and the content type.
request: We create an HTTP POST request with the endpoint, method, and payload.
request.setHeader(): We set the request headers.
We send the HTTP request to HubSpot to create the contacts.
response: We receive the response from HubSpot.
We check the response status code. If it’s 200, the contacts were successfully created in
HubSpot. Otherwise, we log the status
In Part 2 of our HubSpot-Salesforce integration series, we’ve learned how to import contacts
from Salesforce Contact objects into HubSpot Contact objects. This bidirectional data flow
empowers your organization to make the most of both platforms, ensuring a holistic view of
your contacts and enhancing marketing and sales efforts