Last active
December 3, 2024 16:36
-
-
Save HubSpotHanevold/f6f0db18d8592f9256f41172cff7cc60 to your computer and use it in GitHub Desktop.
A php script to replicate HubSpot properties created on a standard object and add to a custom object. Only creates non-HubSpot default properties on the new object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// PHP SCRIPT TO MOVE PROPERTIES FROM A STANDARD OBJECT TO A CUSOTM OBJECT | |
// READ ME | |
// MODIFY THESE VARIABLES BELOW AS YOU SEE FIT (LINES 8-10) | |
// ALSO MODIFY THE GROUP NAME MAPPINGS BELOW (LINES 53-56) | |
$authToken = 'ADD_YOUR_TOKEN_HERE'; | |
$standardObject = 'contacts'; | |
$customObjectInternalName = '2-37668321'; | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "https://api.hubapi.com/crm/v3/properties/$standardObject", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => 'GET', | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer $auth_token" | |
), | |
)); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
// Decode JSON into an associative array | |
$data = json_decode($response, true); | |
// Check if the data is valid and 'results' exists | |
if (!isset($data['results']) || !is_array($data['results'])) { | |
die("Invalid or unexpected response from the API."); | |
} | |
// Filter results where hubspotDefined is not set (unknown) - this removes HubSpot default properties from the standard object you're retrieving properties from | |
$filteredResults = array_filter($data['results'], function ($result) { | |
return !array_key_exists('hubspotDefined', $result); | |
}); | |
// Remove specified keys - these keys don't need to be used in creation of new properties on the custom object | |
$keysToRemove = ['modificationMetadata', 'updatedUserId', 'createdUserId', 'displayOrder', 'updatedAt', 'createdAt']; | |
foreach ($filteredResults as &$result) { | |
// Remove specified keys | |
foreach ($keysToRemove as $key) { | |
unset($result[$key]); | |
} | |
// REPLACE GROUPNAME VALUES HERE! This is a simple search and replace to map the group names from the standard object to the custom object. | |
if (isset($result['groupName']) && $result['groupName'] === 'contactinformation') { | |
$result['groupName'] = 'custom_object_information'; | |
} | |
} | |
// Create new JSON with filtered and cleaned results | |
$filteredJson = json_encode(['results' => array_values($filteredResults)], JSON_PRETTY_PRINT); | |
// Decode the JSON string into an associative array | |
$jsonArray = json_decode($filteredJson, true); | |
// Extract the results array | |
$results = $jsonArray['results']; | |
// Initialize a counter | |
$requestCount = 0; | |
// Loop through each property and make an API request to add to the custom object | |
foreach ($results as $property) { | |
// Prepare the payload for the API request | |
$payload = [ | |
"name" => $property['name'], | |
"label" => $property['label'], | |
"type" => $property['type'], | |
"fieldType" => $property['fieldType'], | |
"groupName" => $property['groupName'], | |
"options" => $property['options'], | |
"calculated" => $property['calculated'], | |
"externalOptions" => $property['externalOptions'], | |
"archived" => $property['archived'], | |
"hasUniqueValue" => $property['hasUniqueValue'], | |
"hidden" => $property['hidden'], | |
"formField" => $property['formField'] | |
]; | |
// Convert the payload to JSON | |
$jsonPayload = json_encode($payload); | |
// Initialize cURL | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_URL => "https://api.hubapi.com/crm/v3/properties/$customObjectInternalName", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 0, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => 'POST', | |
CURLOPT_POSTFIELDS => $jsonPayload, | |
CURLOPT_HTTPHEADER => [ | |
"Content-Type: application/json", | |
"Authorization: Bearer $authToken" | |
] | |
]); | |
// Execute the cURL request | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
// Close the cURL session | |
curl_close($ch); | |
// Log the response for debugging | |
echo "Response for property '{$property['name']}': HTTP Code $httpCode, Response: $response" . PHP_EOL ."<br><hr><br>"; | |
// Increment the request counter | |
$requestCount++; | |
// Pause after every 10 requests to not hit burst limit | |
if ($requestCount % 10 === 0) { | |
sleep(1); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment