Internal Error when creating Association between Contact and Deal with API

Hi,
My Hubspot Database is feeded with external Contacts and Deals.

What is left are the correct Associations between Contacts and Deals.

for that I have createt a script with the following logic:
1. checks if there is already an exicting Association between Contact x and Deal y
2. if no create Association between x and y

I use curl and write in php

it is easy to find the code in the Documentation in Endpoints:

But when executing, Hubspot returns with an internal Error:

array(3) {
 ["status"]=>
 string(5) "error"
 ["message"]=>
 string(14) "internal error"
 ["correlationId"]=>
 string(36) "c65b12f0-05b6-492a-980b-3c4ece2f8d9d"
}

This is the Code im Using:

$assocArray has correct Information about 1 existing Contact and 1 existing Deal

The Access App Token is changed to xxx

var_dump(searchAssociation($assocArray['id_hubspot'], $assocArray['id_hubspot_deal']));
if (!searchAssociation($assocArray['id_hubspot'], $assocArray['id_hubspot_deal']))
{
createAssociation($assocArray['id_hubspot'], $assocArray['id_hubspot_deal']);
}

function createAssociation($fromID, $toID)
{
 $url ='https://api.hubapi.com/crm/v4/objects/contacts/'.$fromID. '/associations/deals/'.$toID;
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('authorization: Bearer xxx'));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);
 $resultarray = json_decode($result, true);
 var_dump($resultarray);
}

Maybe somebody can help me wtih this Error?

What does this Error stand for?

Thank you^^

Hi, @FabianG0 :waving_hand: Thanks for reaching out! Hey, @skimura, do you have any insights or thoughts on troubleshooting for @FabianG0?

Best,

Jaycee

@FabianG0

Hi.

In my case, same error has happen.(use [HubSpot Developer Docs] test tool)

so, ‘v4’ might has bug.

How about use API ‘v3’?

$associationType = 'contact_to_deal';
// almost '{fromObject}_to_{toObject}'

$url ='https://api.hubapi.com/crm/v3/objects/contacts/'.$fromID. '/associations/deals/'.$toID . '/' . $associationType;
// '/crm/v3/objects/{fromObject}/{fromObjectId}/associations/{toObjectType}/{toObjectId}/{associationType}'

Thanks.

@FabianG0

Hi

I think i found a solution.

This api has to set ‘associateType’ in body param.

$url ='https://api.hubapi.com/crm/v4/objects/contacts/'.$fromID.'/associations/deals/'.$toID;

$params = [
 [
 "associationCategory" => "HUBSPOT_DEFINED",
// https://legacydocs.hubspot.com/docs/methods/crm-associations/crm-associations-overview
// Contact to deal => 4
 "associationTypeId" => 4,
 ]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authorization: Bearer xxx'));
// add
curl_setopt($ch, CURLOPT_POSTFILEDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
...

Thanks.

Oh, I forgot to answer…
For me the final step needed for the api call to work, was to add
‘content-type: application/json’
to the header of the curl.

Happy New Year and thank you for helping!