In case you’re looking for the first article about creating a contact in HubSpot API, click here.
While HubSpot is renowned for its powerful automations and seamless contact management, there might be times when its standard features don’t meet your specific needs. One such scenario is adding multiple companies to a contact, a task that HubSpot’s forms do not natively support. Fear not, as HubSpot’s API comes to the rescue, allowing for such customizations.
This guide will walk you through setting up your PHP environment and writing code to add multiple companies in HubSpot using a private app. Before diving in, ensure you’re comfortable with PHP fundamentals and familiar with Composer, PHP’s dependency manager.
use HubSpot\Client\Crm\Companies\Model\BatchInputSimplePublicObjectInputForCreate;
use HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate;
class Company
{
private $client;
private array $ids;
private $company;
private $properties;
public function __construct($client)
{
$this->client = $client;
}
public function create($companiesProperties)
{
foreach ($companiesProperties as $property) {
$simplePublicObjectInput = new SimplePublicObjectInputForCreate([
'properties' => $property
]);
$simplePublicObjectInputs[] = $simplePublicObjectInput;
}
$batchInputSimplePublicObjectInputForCreate = new BatchInputSimplePublicObjectInputForCreate(['inputs' => $simplePublicObjectInputs]);
try {
$batchResponse = $this->client->crm()->companies()->batchApi()->create($batchInputSimplePublicObjectInputForCreate);
foreach ($batchResponse->getResults() as $company) {
$this->ids[] = $company->getId();
}
return $batchResponse;
} catch (Exception $e) {
new HSErrorHandler($e->getMessage(), __LINE__ . __FILE__, $e->getCode());
return null;
}
}
public function getCompanyIds()
{
return $this->ids;
}
}
composer init
to start a new PHP project, following the prompts to configure it.composer require hubspot/api-client
.Your primary script will be Company.php
, handling the creation of companies in HubSpot. Similar to the contact management script, we’ll include error handling to catch and log any issues.
The Company.php
file is structured to handle the complexities of creating multiple companies in HubSpot. Here’s a breakdown of its components:
Security is paramount, so we start by ensuring our script isn’t directly accessed:
if (!defined('ABSPATH')) { exit; }
Set your HubSpot access token, which authenticates your API requests. Replace "YOUR_ACCESS_TOKEN"
with your actual token:
define("HUBSPOT_ACCESS_TOKEN", "YOUR_ACCESS_TOKEN");
We’ll use several classes from the HubSpot API client library to interact with the companies section:
use HubSpot\Client\Crm\Companies\Model\BatchInputSimplePublicObjectInputForCreate;
use HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate;
The Company
class encapsulates everything related to company management. It includes methods for creating companies and storing their IDs for further reference.
Here’s an outline of the Company
class:
Company
class with a HubSpot client instance.SimplePublicObjectInputForCreate
instance for each company, then batches these instances for creation. On success, it stores each company’s ID.As with contact management, error handling is crucial for debugging and ensuring the smooth execution of your script. The HSErrorHandler
class can be reused or adapted from the contact management script to log any errors that occur.
To use the Company
class, instantiate it with your HubSpot client, define the properties for each company, and call the create
method. Here’s a simplified example:
$client = Factory::createWithAccessToken(HUBSPOT_ACCESS_TOKEN);
$company = new Company($client);
$companiesProperties = [ // Array of properties for each company ];
$company->create($companiesProperties);
$companyIds = $company->getCompanyIds();
This process initializes the HubSpot client, creates a Company
object, sets properties for multiple companies, and attempts to create these companies in HubSpot. The returned company IDs can be used for further actions or integrations.
By following this guide, you can extend HubSpot’s capabilities to fit your unique business needs, particularly when dealing with multiple companies for a single contact. For more advanced features or assistance, the HubSpot developer documentation and community forums are invaluable resources.
Learn More About APIs in HubSpot: https://developers.hubspot.com/docs/api/crm/companies
HubSpot Packages: https://packagist.org/packages/hubspot/api-client