SevenShores\Hubspot\Resources\Deals::associateWithContact PHP Method

associateWithContact() public method

public associateWithContact ( integer $dealId, integer | int[] $contactIds ) : mixed
$dealId integer
$contactIds integer | int[]
return mixed
    function associateWithContact($dealId, $contactIds)
    {
        $endpoint = "https://api.hubapi.com/deals/v1/deal/{$dealId}/associations/CONTACT";
        $queryString = build_query_string(['id' => (array) $contactIds]);
        return $this->client->request('put', $endpoint, [], $queryString);
    }

Usage Example

Example #1
0
 /**
  * @test
  */
 public function associateWithContact()
 {
     $dealId = $this->createDeal()->dealId;
     $firstContactId = $this->createContact();
     $secondContactId = $this->createContact();
     $thirdContactId = $this->createContact();
     $response = $this->deals->associateWithContact($dealId, [$firstContactId, $secondContactId, $thirdContactId]);
     $this->assertSame(204, $response->getStatusCode());
     //Check what was associated
     $response = $this->deals->getById($dealId);
     $associatedContacts = $response->associations->associatedVids;
     $expectedAssociatedContacts = [$firstContactId, $secondContactId, $thirdContactId];
     //sorting as order is not predicatable
     sort($associatedContacts);
     sort($expectedAssociatedContacts);
     $this->assertEquals($expectedAssociatedContacts, $associatedContacts);
     //Now disassociate
     $response = $this->deals->disassociateFromContact($dealId, [$firstContactId, $thirdContactId]);
     $this->assertSame(204, $response->getStatusCode());
     //Ensure that only one associated contact left
     $response = $this->deals->getById($dealId);
     $this->assertSame([$secondContactId], $response->associations->associatedVids);
 }