#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).
#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 )
#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] =>
)
*/
#Companies
Companies in HubSpot are special in that they are not considered a custom object type, but instead have their own API (accessible at https://api.hubapi.com/crm/v3/objects/companies
). The example below runs when a new user registers, or an existing user updates their profile, and syncs any configured fields to the user’s associated HubSpot company. If a company does not exist yet for the user, one will be created.
This is a very simple example and would need additional logic to handle situations where a new user is joining an existing company, and for allowing changing a user’s company, but we hope it’s useful as a starting place.
You may also want to look into automatically creating and associating companies with contacts.
/**
* Create or update a HubSpot company for a user.
*
* @param int $user_id The user ID.
* @param string $contact_id The contact ID.
* @param array $user_meta The user meta.
* @return int|false The company ID, or false if the company could not be created.
*/
function create_or_update_hubspot_company( $user_id, $contact_id, $user_meta ) {
// The static data to update, or pull it from $user_meta.
$company_data = array(
'name' => 'Company Name',
'domain' => 'company.com',
// 'property_one' => 'Value one',
// 'property_two' => 'Value two',
// 'property_three' => $user_meta['user_custom_field'],
);
$update_data = array(
'properties' => $company_data,
'associations' => array(
array(
'to' => array(
'id' => $contact_id,
),
'types' => array(
array(
'associationCategory' => 'HUBSPOT_DEFINED',
'associationTypeId' => 280, // "Company to Contact".
),
),
),
),
);
// Get the authorization headers from WP Fusion.
$params = wp_fusion()->crm->get_params();
$params['body'] = wp_json_encode( $update_data );
// See if we have a company ID already.
$company_id = get_user_meta( $user_id, 'hubspot_company_id', true );
if ( empty( $company_id ) ) {
// New company.
$request = 'https://api.hubapi.com/crm/v3/objects/companies';
} else {
// Update existing company.
$request = 'https://api.hubapi.com/crm/v3/objects/companies/' . $company_id;
$params['method'] = 'PUT';
}
// Send the API call.
$response = wp_remote_post( $request, $params );
if ( is_wp_error( $response ) ) {
wpf_log( 'error', $user_id, 'Error adding company to HubSpot: ' . $response->get_error_message() );
return;
}
$response = json_decode( wp_remote_retrieve_body( $response ) );
// Save the company ID to the user meta.
update_user_meta( $user_id, 'hubspot_company_id', $response->id );
return $response->id;
}
add_action( 'wpf_pushed_user_meta', 'create_or_update_hubspot_company', 10, 3 );
add_action( 'wpf_user_created', 'create_or_update_hubspot_company', 10, 3 );