phplaravelhubspot

hubspot-api-php associate a contact to a company error: Invalid input JSON on line 1, column 1: Cannot deserialize value of type


Stack

Problem:

I can create a contact and a company just fine in Hubspot via the SDK -> API, however I can't create an association between a contact and company which you seemingly have to do manually.

Code:

// Create new user
$contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInputForCreate();
$contactInput->setProperties([
    'firstname' => 'Jonny',
    'lastname' => 'Cage',
    'email' => 'johnny.cage@email.com',
    'phone' => '123456',
    'company' => 'Acme inc'
]);
$contact = HubSpot::crm()->contacts()->basicApi()->create($contactInput);

// Create new company
$companyInput = new \HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate();
$companyInput->setProperties([
    'name' => 'Acme inc', 
    'country' => 'United Kingdom',
    'city' => 'London',
]);
$company = HubSpot::crm()->companies()->basicApi()->create($companyInput);

// Associate the user to the company
// https://developers.hubspot.com/docs/api/crm/associations

/*
* https://github.com/HubSpot/hubspot-api-php/blob/master/codegen/Crm/Associations/V4/Model/AssociationSpec.php#L43
* @param mixed[] $data Associated array of property values
*/
$association_spec = new \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec([
    'association_category' => \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec::ASSOCIATION_CATEGORY_HUBSPOT_DEFINED,
    'association_type_id' => 1, // Contact to company (Primary)
]);
// dd($associationSpec->valid()); // this returns true

/* 
* https://github.com/HubSpot/hubspot-api-php/blob/master/codegen/Crm/Associations/V4/Api/BasicApi.php#L451
* @param  string $object_type object_type (required)
* @param  string $object_id object_id (required)
* @param  string $to_object_type to_object_type (required)
* @param  string $to_object_id to_object_id (required)
* @param  \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec[] $association_spec association_spec (required)
* @param  string $contentType The value for the Content-Type header. Check self::contentTypes['create'] to see the possible values for this operation
*/
$association = Hubspot::crm()->associations()->v4()->basicApi()->create('contacts', $contact->getId(), 'companies', $company->getId(), $association_spec);

Response:

[400] Client error: `PUT https://api.hubapi.com/crm/v4/objects/contact/xxx/associations/company/xxxxxx` resulted in a `400 Bad Request` response:


{"status":"error","message":"Invalid input JSON on line 1, column 1: Cannot deserialize value of type `java.util.HashSet (truncated...)

Notes

I would have thought this to be a super common action so I'm surprised that there seemingly is very little information or examples of how to do this online? And a lot of the information I have found seems to be out of date. Or have I just missed something really obvious? Either way, I'd really appreciate some help to figure out what I'm doing wrong.


Solution

  • As @Frédéric Clausset pointed out (thank you!), I had missed that I needed to pass an array of AssociationSpec[] through to this function:

    Hubspot::crm()->associations()->v4()->basicApi()->create();
    

    I then got an error telling me I had missing values set in the AssociationSpec object - so I found and used the setters rather than trying to set them in the constructor, and then it worked!

    Here is the final code that worked for me at the time of writing:

    // Create new user
    $contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInputForCreate();
    $contactInput->setProperties([
        'firstname' => 'Jonny',
        'lastname' => 'Cage',
        'email' => 'johnny.cage@email.com',
        'phone' => '123456',
        'company' => 'Acme inc'
    ]);
    $contact = HubSpot::crm()->contacts()->basicApi()->create($contactInput);
    
    // Create new company
    $companyInput = new \HubSpot\Client\Crm\Companies\Model\SimplePublicObjectInputForCreate();
    $companyInput->setProperties([
        'name' => 'Acme inc', 
        'country' => 'United Kingdom',
        'city' => 'London',
    ]);
    $company = HubSpot::crm()->companies()->basicApi()->create($companyInput);
    
    // Associate the user to the company
    // https://developers.hubspot.com/docs/api/crm/associations
    
    $associationSpec = new \HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec();
    $associationSpec->setAssociationCategory(\HubSpot\Client\Crm\Associations\V4\Model\AssociationSpec::ASSOCIATION_CATEGORY_HUBSPOT_DEFINED);
    $associationSpec->setAssociationTypeId(1);
    
    $association = Hubspot::crm()->associations()->v4()->basicApi()->create('contact', $contact->getId(), 'company', $company->getId(), [$associationSpec]);
    

    On we go...