Custom Objects with HubSpot

#Overview

In addition to supporting custom objects with Zoho, Salesforce, and Ontraport via the wpf_crm_object_type filter and the add_object() method, WP Fusion supports adding and updating custom objects with HubSpot.

For a video walkthrough of custom objects, check out Data in HubSpot: Custom Objects and Other Tools in HubSpot’s Academy.

For additional information and examples please review HubSpot’s CRM Custom Objects documentation.

The examples below assume you have already created a custom object schema and know the object type ID as well as any association type IDs (if you plan to associate your objects with other objects).

#Authentication

Because custom objects are still in beta, your HubSpot API key is required to interact with them (your existing OAuth authorization via WP Fusion isn’t yet sufficient).

To use custom objects with HubSpot and WP Fusion, you must first define your HubSpot API key in your wp-config.php file, like so:

define( 'HUBSPOT_API_KEY', 'xx599590-7888-43ed-a896-5abbc2ef9aa2' );

#Methods

The methods are basically the same as those with other CRMs, those are:

  • add_object( $properties, $object_type_id )
  • update_object( $object_id, $properties, $object_type_id )
  • load_object( $object_id, $object_type_id, $properties )

#add_object()

This method creates a new object of the specified object type ID. Following the HubSpot custom objects walkthrough, we have created a new Car custom object, with an objectTypeId of 2-4370788.

$properties = array(
	'condition'     => 'used',
	'date_received' => '1582416000000',
	'year'          => '2014',
	'make'          => 'Nissan',
	'model'         => 'Frontier',
	'vin'           => '4Y1SL65848Z411439',
	'color'         => 'White',
	'mileage'       => '80000',
	'price'         => '12000',
	'notes'         => 'Excellent condition. No accidents.',
);

$object_type_id = '2-4370788';

$object_id = wp_fusion()->crm->add_object( $properties, $object_type_id );

if ( is_wp_error( $object_id ) ) {
	wpf_log( 'error', wpf_get_current_user_id(), 'Error adding object: ' . $object_id->get_error_message() );
	return false;
}

// Do what you want with $object_id here.

Now we have an $object_id with the ID of the new object. This can then be used for additional operations.

For example to associate the new object with a contact ID 101, we can make a PUT request against the associations API:

$contact_id          = '101';
$association_type_id = '3';

$request = "https://api.hubapi.com/crm/v3/objects/{$object_type_id}/{$object_id}/associations/contacts/{$contact_id}/{$association_type_id}/?hapikey=" . HUBSPOT_API_KEY;

$params   = array( 'method' => 'PUT' );
$response = wp_safe_remote_request( $request, $params );

if ( is_wp_error( $response ) ) {
	wpf_log( 'error', wpf_get_current_user_id(), 'Error associating object with contact: ' . $response->get_error_message() );
	return false;
}

Then the new object is associated with the contact, like so:

#update_object()

This method allows you to update an existing object. For example to change our Nissan Frontier with ID 599237525 into a Ford Ranger:

$properties = array(
	'make'  => 'Ford',
	'model' => 'Ranger',
);

$object_id      = '599237525';
$object_type_id = '2-4370788';

$response = wp_fusion()->crm->update_object( $object_id, $properties, $object_type_id );

if ( is_wp_error( $response ) ) {
	wpf_log( 'error', wpf_get_current_user_id(), 'Error updating object: ' . $response->get_error_message() );
	return false;
}

#load_object()

This method loads an object by ID and returns its properties. Note that unlike other CRMs, you must specify the properties you want returned.

Using our Car example from above, we can request the Condition, Year, Make, and Model:

$object_id      = '599237525';
$object_type_id = '2-4370788';
$properties     = array( 'condition', 'year', 'make', 'model' );

$response = wp_fusion()->crm->load_object( $object_id, $object_type_id, $properties );

if ( is_wp_error( $response ) ) {
	wpf_log( 'error', wpf_get_current_user_id(), 'Error loading object: ' . $response->get_error_message() );
	return false;
}

print_r( $response );

/*

Array
(
	[id] => 599237525
	[properties] => Array
		(
			[condition] => used
			[hs_createdate] => 2021-12-15T09:41:24.159Z
			[hs_lastmodifieddate] => 2021-12-15T10:10:22.801Z
			[hs_object_id] => 599237525
			[make] => Ford
			[model] => Ranger
			[year] => 2014
		)

	[createdAt] => 2021-12-15T09:41:24.159Z
	[updatedAt] => 2021-12-15T10:10:22.801Z
	[archived] =>
)

*/

Was this helpful?