basecrm-php icon indicating copy to clipboard operation
basecrm-php copied to clipboard

upsert method

Open patrickwarner opened this issue 9 years ago • 3 comments

There does not seem to be a method which allows you to call the upsert API call.

Create a new contact or update an existing, based on a value of a filter or a set of filters. At least a single filter - query parameter - is required. If no parameters are present, the request will return an error.

patrickwarner avatar Mar 02 '16 23:03 patrickwarner

UP

andriytkachiv avatar Apr 13 '16 10:04 andriytkachiv

Anybody working on this? I'll very soon need to implement leads->upsert. I would be willing to contribute if nobody has already started working on it.

FYI, here's the leads upsert docs, which have examples for only curl, ruby, and python.

quinncomendant avatar Jan 25 '17 11:01 quinncomendant

Until there is an official release of an upsert method, I've gotten this to work by adding this method to class ContactsService:

/**
 * Upsert a contact
 *
 * post '/contacts/upsert?{filters}'
 *
 * Create a new contact or update an existing, based on a value of a filter
 * or a set of filters. At least a single filter - query parameter - is
 * required. If no parameters are present, the request will return an error.
 *
 * @param array $filters A set of attributes that select an existing record to update. (If $filters is left empty, this function will behave the same as create().)
 * @param array $contact This array's attributes describe the object to be updated.
 *
 * @return array The resulting object representing updated resource.
 */
public function upsert(array $filters, array $contact)
{
  $attributes = array_intersect_key($contact, array_flip(self::$keysToPersist));
  if (isset($attributes['custom_fields']) && empty($attributes['custom_fields'])) unset($attributes['custom_fields']);

  if (empty($filters)) {
      list($code, $upsertedContact) = $this->httpClient->post("/contacts", $attributes);
  } else {
      list($code, $upsertedContact) = $this->httpClient->post(sprintf("/contacts/upsert?%s", http_build_query($filters)), $attributes);
  }

  return $upsertedContact;
}

And used like this:

$filter = [
	'email' => '[email protected]',
];
$contact = [
	'email' => '[email protected]',
    'name' => 'John Doe',
	'phone' => '1111111111111',
	'custom_fields' => [
		'Custom A' => '…',
		'Custom B' => '…',
	]
];

$basecrm = new \BaseCRM\Client($config);
$basecrm->contacts->upsert($filter, $contact);

quinncomendant avatar May 08 '17 16:05 quinncomendant