Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HubSpotHanevold/a6f699e0fae546e29baffda26fa1c66e to your computer and use it in GitHub Desktop.
Save HubSpotHanevold/a6f699e0fae546e29baffda26fa1c66e to your computer and use it in GitHub Desktop.
A php webhook endpoint that will append a multi-picklist with missing values. Also, returns the property update values in an easy way to use in a custom code action later in the workflow.
<?php
// RETRIEVE THE CURRENT DROPDOWN VALUES FOR THE LINE-ITEM PROPERTY
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.hubapi.com/crm/v3/properties/deal/line_items',
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 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
),
));
$response = curl_exec($curl);
curl_close($curl);
// PARSE THROUGH THE VALUES OF THE MULTI-DROPDOWN
$property_data = json_decode($response, true);
$options = $property_data['options'];
$count_options = count($options);
$values_array = array();
$y = 0;
while($y < $count_options) {
$values_array[] = $property_data['options'][$y]['value'];
$y++;
}
// GET POSTED JSON DATA
$json = file_get_contents('php://input');
// GET THE POSTED DATA FROM THE WEBHOOK
$data = json_decode($json, true);
$record_id = $data['hs_object_id'];
$line_item_array = $data['line_items_json'];
$count_line_items = count($line_item_array);
// CHECK IF THE LINE-ITEM DATA IS IN THE DROPDOWN LIST OR NOT, IF NOT ADD
$x = 0;
$missing_values_array = array();
while($x < $count_line_items) {
$line_item = $data['line_items_json'][$x]['name'];
$line_item_str .= $data['line_items_json'][$x]['name'] . ';';
if (in_array($line_item, $values_array)) {
// NEW ITEM IN DROPDOWN NOT NEEDED
// NO ACTION
} else {
// BUILD AN ARRAY TO ADD NEW LINE-ITEMS TO DROPDOWN
$missing_values_array[] = $line_item;
}
$x++;
}
$count_non_value = count($missing_values_array);
if($count_non_value > 0) {
ob_start();
$count_value = count($values_array);
$v = 0;
while($v < $count_value) {
echo '{';
echo '"label": "'.$values_array[$v].'",';
echo '"value": "'.$values_array[$v].'"';
echo '},';
$v++;
}
$z = 0;
while($z < $count_non_value) {
echo '{';
echo '"label": "'.$missing_values_array[$z].'",';
echo '"value": "'.$missing_values_array[$z].'"';
echo '},';
$z++;
}
// SET THE PAYLOAD AS A SINGLE VARIABLE
$payload = ob_get_contents();
ob_end_clean();
ob_end_flush();
$payload = rtrim($payload, ",");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.hubapi.com/crm/v3/properties/deals/line_items',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS =>'{
"options": [
'.$payload.'
]
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
}
echo '{';
echo '"line_items": "'. $line_item_str.'"';
echo '}';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment